How to use long id in Rails applications?

后端 未结 11 1532
天涯浪人
天涯浪人 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

    Correction to how to change the default primary key column type:

    Instead of:

    ActiveRecord::ConnectionAdapters::MysqlAdapter::NATIVE_DATABASE_TYPES[:primary_key] = "BIGINT UNSIGNED DEFAULT NULL auto_increment PRIMARY KEY"
    

    you should do:

    ActiveRecord::ConnectionAdapters::MysqlAdapter::NATIVE_DATABASE_TYPES[:primary_key] = "BIGINT(8) UNSIGNED DEFAULT NULL auto_increment PRIMARY KEY"
    

    or else you won't be able to add foreign key restrictions in the database layer.

提交回复
热议问题