簡體   English   中英

在Rails中使用add_reference時如何使用bigint類型?

[英]How to use bigint type when using add_reference with Rails?

在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方法?

我找到了答案:

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

它有type選項。 這就是我想要的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM