rails3 bigint主键

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也是如此.

ska*_*rou 5

我有同样的问题。我认为最简单的表格方式

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)

  • @skalogirou 我刚刚做了`change_column :my_primary, :id, 'bigint'` 并且工作正常,没有丢失自动增量(在 Rails 4、Postgres 上) (4认同)
  • 差不多好了。只需执行`change_column :accounts, :id, 'bigint'` (2认同)

mhe*_*xon 3

不久前我自己也在这里找到了答案:Using Rails, how can I set my Primary Key to not be an integer-typed columns?

您需要设置primary_key: false,然后在执行迁移之前使用自定义语句。

编辑1:您需要检查数据库文档以了解要执行的确切查询。它作为常规 SQL 语句执行,并且需要特定于数据库。我提到的问题中的示例是针对 Postgre SQL 的。如果您使用的是 MySQL,则可能需要更改它。