且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

迁移中没有自动增量选项的 id 字段

更新时间:2022-06-04 08:49:13

为了记录,如果您绝对需要这样做(它不应该经常发生),这里是使用Rails 迁移 DSL:

For the record, if you absolutely need to do this (it shouldn't happen often), here's the way to do a non-autoincrementing primary key with the Rails migration DSL:

create_table(:table_name, :id => false) do |t|
  t.integer :id, :options => 'PRIMARY KEY'
end

无论如何这都适用于 MySQL,如果您的数据库使用不同的语法来指定主键,请替换 :options =>'PRIMARY KEY' 和任何东西.

That will work for MySQL anyway, if your DB uses different syntax to specify a primary key, replace the :options => 'PRIMARY KEY' with whatever that is.