How to turn off BIGINT primary keys in Rails 5.1

元气小坏坏 提交于 2019-12-10 15:10:37

问题


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:

  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.


来源:https://stackoverflow.com/questions/44390225/how-to-turn-off-bigint-primary-keys-in-rails-5-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!