【问题标题】:How to use bigint type when using add_reference with Rails?在 Rails 中使用 add_reference 时如何使用 bigint 类型?
【发布时间】:2017-03-16 08:26:06
【问题描述】:

在 Rails 5 和 PostgreSQL 中。

我创建此文件是为了默认使用bigint 数据类型而不是integer

# config/initializers/bigint_primary_keys.rb
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::NATIVE_DATABASE_TYPES[:primary_key] = 'bigserial primary key'

所以当运行 create 和 migrate 时,表 id 将是 bigint 类型。

# posts table
id bigint not null
...

# users table
id bigint not null
...

但是在迁移中使用add_reference时:

class ChangeA < ActiveRecord::Migration[5]
  def change
    add_reference :posts, :users
  end
end

posts 表添加此字段:

# posts table
...
user_id integer

postsusers 的表id 都是bigint 类型,但是这个字段没有改变。

有没有办法将数据类型设置为add_reference 方法?

【问题讨论】:

    标签: ruby-on-rails postgresql


    【解决方案1】:

    我找到了答案:

    http://apidock.com/rails/ActiveRecord/ConnectionAdapters/SchemaStatements/add_reference

    它有type 选项。这就是我想要的。

    【讨论】:

      猜你喜欢
      • 2019-11-03
      • 1970-01-01
      • 2012-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-05
      相关资源
      最近更新 更多