Rails3 bigint 主键
我想在 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 也是如此。
I would like to create a bigint
(or string
or whatever that is not int
) typed primary key field under Rails 3.
I have a given structure of data, for example:
things
------
id bigint primary_key
name char(32)
The approach I'm currently trying to push:
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
The column type will be correct, but the primary key option will not be present with sqlite3 and I suspect that this is the case for MySQL too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我也有同样的问题。我认为表
最简单的方法
是
I had the same problem. I think the easiest way for a table
is
不久前我自己也有这个问题,并在这里找到了答案:使用Rails,如何将主键设置为不是整数类型列?
您需要设置primary_key: false,然后使用执行迁移之前的自定义语句。
编辑1:您需要检查数据库文档以了解要执行的确切查询。它作为常规 SQL 语句执行,并且需要特定于数据库。我提到的问题中的示例是针对 Postgre SQL 的。如果您使用的是 MySQL,则可能需要更改它。
Had that myself not long ago and found the answer here: Using Rails, how can I set my primary key to not be an integer-typed column?
You need to set primary_key: false and then use a custom statement before you execute the migration.
EDIT 1: You need to check your database docs for the exact query to perform. It is executed as a regular SQL statement and needs to be database specific. The example in the question I referred to is for Postgre SQL. If you are on MySQL you might have to change that.
对于 MySQL,您可以使用“SERIAL”,它是“BIGINT UNSIGNED NOT NULL AUTO_INCRMENT”的别名
For MySQL you can use "SERIAL" which is alias for "BIGINT UNSIGNED NOT NULL AUTO_INCREMENT"
对于那些来到这里(像我一样)努力弄清楚如何制作使用
bigint
而不是int
的自定义id
列的人code>,我没有意识到的是,对于 Rails 5.1 及更高版本,bigint
是id
https://github.com/rails/rails/pull/26266
For those of you who came here (like I did) in an effort to figure out how to make a custom
id
column that usesbigint
instead ofint
, what I didn't realize is that for Rails 5.1 and above,bigint
is the default type forid
https://github.com/rails/rails/pull/26266
skalogirou 的答案很好,但更改不会反映在 schema.rb 中。因此像 db:schema:load 和 db:test:clone 这样的任务不会创建相同的数据库结构。
所需的解决方法是增强 db:schema:load 和 db:test:clone rake 任务,如下所述:
http://www.lshift.net/blog/2013/09/30/changing-the-primary-key-type-in-ruby-on-rails-models/
这是我用的基于该解决方法:
skalogirou's answer is good but the change will not be reflected in schema.rb. So the tasks like
db:schema:load
anddb:test:clone
will not create identical DB structure.The required workaround is to enhance
db:schema:load
and db:test:clone rake tasks as described here:http://www.lshift.net/blog/2013/09/30/changing-the-primary-key-type-in-ruby-on-rails-models/
This is what I used based on that workaround:
如果你想转换 Postgres 中的所有表,你将需要运行此代码
If you want to convert all tables in Postgres you will need to run this code