Not*_*ist 8 ruby-on-rails primary-key bigint ruby-on-rails-3
我想在Rails 3下创建一个bigint(或string任何不是int)类型的主键字段.
我有一个给定的数据结构,例如:
things
------
id bigint primary_key
name char(32)
Run Code Online (Sandbox Code Playgroud)
我目前正试图推动的方法:
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
Run Code Online (Sandbox Code Playgroud)
列类型是正确的,但主键选项不会出现在sqlite3中,我怀疑MySQL也是如此.
我有同样的问题。我认为最简单的表格方式
accounts
id bigint primary key
name char Run Code Online (Sandbox Code Playgroud)
是
create_table :accounts do |t|
t.string :name
end
change_column :accounts, :id , "bigint NOT NULL AUTO_INCREMENT"Run Code Online (Sandbox Code Playgroud)
不久前我自己也在这里找到了答案:Using Rails, how can I set my Primary Key to not be an integer-typed columns?
您需要设置primary_key: false,然后在执行迁移之前使用自定义语句。
编辑1:您需要检查数据库文档以了解要执行的确切查询。它作为常规 SQL 语句执行,并且需要特定于数据库。我提到的问题中的示例是针对 Postgre SQL 的。如果您使用的是 MySQL,则可能需要更改它。
| 归档时间: |
|
| 查看次数: |
8669 次 |
| 最近记录: |