如何在 Rails 5.1 中关闭 BIGINT 主键 [英] How to turn off BIGINT primary keys in Rails 5.1

查看:46
本文介绍了如何在 Rails 5.1 中关闭 BIGINT 主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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:

  1. Migration[5.0]ActiveRecord::ConnectionAdapters::TableDefinition
  2. 创建匿名模块
  3. 在第一个中定义create_tableadd_referenceadd_belongs_toreferencesbelongs_to> 在第二个中(belongs_to 应该只是 references 的别名)
  4. 在这些方法中,只需修改选项并调用 super.不要忘记处理签名!
  5. 将这些模块添加到它们各自的类中将处理一切为了你.
  6. 您还可以做得更好,也可以为他们的移除对手执行此操作.
  1. Create anonymous modules for Migration[5.0] and ActiveRecord::ConnectionAdapters::TableDefinition
  2. Define create_table, add_reference, add_belongs_to in first one, references and belongs_to in second one (belongs_to ones should be just aliases of references)
  3. In those methods just modify options and call super. Don't forget to handle signatures!
  4. Prepending those modules to their respective classes will handle everything for you.
  5. You can go even better and do this for their removal counterparts too.

这篇关于如何在 Rails 5.1 中关闭 BIGINT 主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆