问题
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?
回答1:
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:
- Create anonymous modules for
Migration[5.0]
andActiveRecord::ConnectionAdapters::TableDefinition
- Define
create_table
,add_reference
,add_belongs_to
in first one,references
andbelongs_to
in second one (belongs_to
ones should be just aliases ofreferences
) - In those methods just modify options and call super. Don't forget to handle signatures!
- Prepending those modules to their respective classes will handle everything for you.
- You can go even better and do this for their removal counterparts too.
来源:https://stackoverflow.com/questions/44390225/how-to-turn-off-bigint-primary-keys-in-rails-5-1