2017-03-16 30 views
0

在Rails 5和PostgreSQL中。如何在使用add_reference和Rails时使用bigint类型?

我创造了这个文件,要默认使用bigint数据类型,而不是integer

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

所以当运行创建和迁移,表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方法?

回答

相关问题