要设置默认主键列类型,迁移文件不是乱七八糟的地方。
相反,只需将其粘贴在 config/environment.rb 的底部
ActiveRecord::ConnectionAdapters::MysqlAdapter::NATIVE_DATABASE_TYPES[:primary_key] = "BIGINT UNSIGNED DEFAULT NULL auto_increment PRIMARY KEY"
您的所有表都应使用id 的预期列类型创建:
+--------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
在你完成了你打算做的事情之后......下一个问题可能是“如何使我的外键列具有相同的列类型?”因为将主键 people.id 设置为 bigint(20) unsigned 并将 person_id 设置为 int(11) 或其他任何内容都没有意义?
对于这些列,您可以参考其他建议,例如
t.column :author_id, 'BIGINT UNSIGNED'
t.integer :author_id, :limit => 8
更新:@Notinlist,要在任意表上使用任意列作为主键,您需要执行 create_table-change_column 舞蹈:
create_table(:users) do |t|
# column definitions here..
end
change_column :users, :id, :float # or some other column type
例如如果我想要guid 而不是自增整数,
create_table(:users, :primary_key => 'guid') do |t|
# column definitions here..
end
change_column :users, :guid, :string, :limit => 36