How to disable BIGINT primary keys in Rails 5.1

Rails 5.1 migration generates BIGINT

(instead of Integer

) for primary keys of tables ( changelog ).

Can I disable this somewhere in the config? If so, how do I disable it?

+3


source to share


1 answer


As per pull request , this is not possible at the configuration level. But you can force force to id

be an integer like:

create_table :users, id: :integer do

      

On the other hand, you should be aware that the changes also affected the behavior references

, so you should be careful with them:



t.references :orders, type: :integer

      

Seeing that this is too much repetitive code, I suggest you write helpers for this, override the default methods, or be very radical and run your database adapter by changing it to whatever you like. I would go with the second option:

  • Create anonymous modules for Migration[5.0]

    andActiveRecord::ConnectionAdapters::TableDefinition

  • Identify create_table

    , add_reference

    , add_belongs_to

    in the first references

    and belongs_to

    in the second ( belongs_to

    must be a pseudonym references

    )
  • In these methods, just change the parameters and call super. Don't forget to handle signatures!
  • Providing these modules to the appropriate classes will be all for you.
  • You can go even better and do it for your removal colleagues.
+4


source







All Articles