【发布时间】: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
posts 和users 的表id 都是bigint 类型,但是这个字段没有改变。
有没有办法将数据类型设置为add_reference 方法?
【问题讨论】: