我想在Rails 3下创建一个bigint
(或string
或任何不是int
)类型的主键字段。
我有给定的数据结构,例如:
things
------
id bigint primary_key
name char(32)
我目前正在尝试的方法:
create_table :things, :id => false do |t| # That prevents the creation of (id int) PK
t.integer :id, :limit => 8 # That makes the column type bigint
t.string :name, :limit => 32
t.primary_key :id # This is perfectly ignored :-(
end
列类型将是正确的,但sqlite3不会提供主键选项,我怀疑MySQL也是这种情况。
最佳答案
不久前有我自己,并在这里找到了答案:Using Rails, how can I set my primary key to not be an integer-typed column?
您需要设置primary_key:false,然后使用自定义语句来执行迁移。
编辑1:您需要检查数据库文档以查找要执行的确切查询。它作为常规SQL语句执行,并且需要特定于数据库。我提到的问题中的示例是针对Postgre SQL的。如果您使用的是MySQL,则可能必须更改它。
关于ruby-on-rails - rails3 bigint主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5880207/