Rails 4 迁移:创建 mysql table 有主键但没有 auto_increment
Rails 4 migration: Create mysql table with primary key but without auto_increment
可能重复 How to turn off auto_increment in Rails Active Record
rails 的新手,我想创建一个 fm_zipcodes
table zipcode
作为主键,但我不想要额外的 auto_increment
。我尝试了不同的方法,但 none 对我有用,auto_increment
没有反映在 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'
create_table :fm_zipcodes do |t|
t.integer :zipcode, null: false
# ...
end
add_foreign_key :fm_zipcodes, :zipcode
可能重复 How to turn off auto_increment in Rails Active Record
rails 的新手,我想创建一个 fm_zipcodes
table zipcode
作为主键,但我不想要额外的 auto_increment
。我尝试了不同的方法,但 none 对我有用,auto_increment
没有反映在 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'
create_table :fm_zipcodes do |t|
t.integer :zipcode, null: false
# ...
end
add_foreign_key :fm_zipcodes, :zipcode