gpt4 book ai didi

mysql - Rails 4 迁移 : Create mysql table with primary key but without auto_increment

转载 作者:行者123 更新时间:2023-11-29 11:44:26 25 4
gpt4 key购买 nike

可能重复How to turn off auto_increment in Rails Active Record

rails 新手,我想创建一个 fm_zipcodeszipcode 作为主键,但我不需要额外的 auto_increment。我尝试了不同的方法,但没有一个对我有用,并且 auto_increment 没有反射(reflect)在 db/schema.rb 文件中。

create_table :fm_zipcodes, :primary_key => :zipcode do |t|
t.integer :state_id, null: false
..
end

关闭答案https://stackoverflow.com/a/17960388/1093542

最佳答案

我会做这样的事情:

# 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com