How to use long id in Rails applications?

后端 未结 11 1546
天涯浪人
天涯浪人 2020-11-30 22:24

How can I change the (default) type for ActiveRecord\'s IDs? int is not long enough, I would prefer long. I was surprised that there is no :long for the migrations - does on

11条回答
  •  感动是毒
    2020-11-30 23:00

    If anyone needs this to work with PostgreSQL, create an initializer like this:

    # config/initializers/bigint_primary_keys.rb
    ActiveRecord::Base.establish_connection
    ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::NATIVE_DATABASE_TYPES[:primary_key] = 'bigserial primary key'
    

    Because of lazy loading in Rails 3.2 (and maybe even earlier versions), ActiveRecord::ConnectionAdapters::PostgreSQLAdapter won't be required until you establish the database connection.

提交回复
热议问题