可能重复How to turn off auto_increment in Rails Active Record
rails 新手,我想创建一个 fm_zipcodes
表 zipcode
作为主键,但我不需要额外的 auto_increment
。我尝试了不同的方法,但没有一个对我有用,并且 auto_increment
没有反射(reflect)在 db/schema.rb
文件中。
create_table :fm_zipcodes, :primary_key => :zipcode do |t|
t.integer :state_id, null: false
..
end
最佳答案
我会做这样的事情:
# in your migration
def up
create_table :fm_zipcodes, id: false do |t|
t.integer :zipcode, null: false
# ...
end
add_index :fm_zipcodes, :zipcode, unique: true
end
# in your FmZipcode model
self.primary_key = 'zipcode'
关于mysql - Rails 4 迁移 : Create mysql table with primary key but without auto_increment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35196983/