Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn off BIGINT primary keys in Rails 5.1

Rails 5.1 migrations generates BIGINT (instead of Integer) for tables' primary keys (changelog).

Is it possible to disable that somewhere in the config? If so, how to do disable it?

like image 857
maicher Avatar asked Jun 06 '17 12:06

maicher


1 Answers

According to pull request, no this is not possible on config level. But you can, in fact, force id to be integer, like this:

create_table :users, id: :integer do

On the other hand, you must be aware that changes also affected references behavior, so you should be careful with those:

t.references :orders, type: :integer

Seeing as this is too much repeated code, I suggest you write helpers for this, override default methods, or be very radical and fork your database adapter, changing this in it as you like. I'd go with the second option:

  1. Create anonymous modules for Migration[5.0] and ActiveRecord::ConnectionAdapters::TableDefinition
  2. Define create_table, add_reference, add_belongs_to in first one, references and belongs_to in second one (belongs_to ones should be just aliases of references)
  3. In those methods just modify options and call super. Don't forget to handle signatures!
  4. Prepending those modules to their respective classes will handle everything for you.
  5. You can go even better and do this for their removal counterparts too.
like image 135
wyde19 Avatar answered Sep 22 '22 10:09

wyde19