How to use long id in Rails applications?

后端 未结 11 1541
天涯浪人
天涯浪人 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 22:59

    Borrowing from other solutions, adjusted for what worked for me recently.

    Add to a file in config/initializers. It declares a new column type (adapted from chookeat's suggestion).

    ActiveRecord::ConnectionAdapters::Mysql2Adapter::NATIVE_DATABASE_TYPES[:long_primary_key] = "BIGINT(20) DEFAULT NULL auto_increment PRIMARY KEY"

    Migrations that use a long id are as such:

        create_table :notification_logs, :id => false do |t|
    
          t.column :id, :long_primary_key
          # ...
        end
    

提交回复
热议问题