[英]No relationship between tables shown in schema diagram when using add_reference in Rails
[英]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
posts
和users
表ID都是bigint
類型,但此字段不會更改。
有沒有辦法將數據類型設置為add_reference
方法?
我找到了答案:
http://apidock.com/rails/ActiveRecord/ConnectionAdapters/SchemaStatements/add_reference
它有type
選項。 這就是我想要的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.