如何在 Rails 5.1 中关闭 BIGINT 主键 [英] How to turn off BIGINT primary keys in Rails 5.1
问题描述
Rails 5.1 迁移为表的主键生成 BIGINT
(而不是 Integer
)(changelog).
Rails 5.1 migrations generates BIGINT
(instead of Integer
) for tables' primary keys (changelog).
是否可以在配置中的某处禁用它?如果是这样,如何禁用它?
Is it possible to disable that somewhere in the config? If so, how to do disable it?
推荐答案
根据pull请求,不,这在配置级别是不可能的.但实际上,您可以强制 id
为整数,如下所示:
According to pull request, no this is not possible on config level.
But you can, in fact, force id
to be integer, like this:
create_table :users, id: :integer do
另一方面,您必须意识到更改也会影响 references
行为,因此您应该小心:
On the other hand, you must be aware that changes also affected references
behavior, so you should be careful with those:
t.references :orders, type: :integer
看到这是太多重复的代码,我建议您为此编写帮助程序,覆盖默认方法,或者非常激进并分叉您的数据库适配器,随心所欲地更改它.我会选择第二个选项:
Seeing as this is too much repeated code, I suggest you write helpers for this, override default methods, or be very radical and fork your database adapter, changing this in it as you like. I'd go with the second option:
- 为
Migration[5.0]
和ActiveRecord::ConnectionAdapters::TableDefinition
创建匿名模块 - 在第一个中定义
create_table
、add_reference
、add_belongs_to
、references
和belongs_to
> 在第二个中(belongs_to
应该只是references
的别名) - 在这些方法中,只需修改选项并调用 super.不要忘记处理签名!
- 将这些模块添加到它们各自的类中将处理一切为了你.
- 您还可以做得更好,也可以为他们的移除对手执行此操作.
- Create anonymous modules for
Migration[5.0]
andActiveRecord::ConnectionAdapters::TableDefinition
- Define
create_table
,add_reference
,add_belongs_to
in first one,references
andbelongs_to
in second one (belongs_to
ones should be just aliases ofreferences
) - In those methods just modify options and call super. Don't forget to handle signatures!
- Prepending those modules to their respective classes will handle everything for you.
- You can go even better and do this for their removal counterparts too.
这篇关于如何在 Rails 5.1 中关闭 BIGINT 主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!