gpt4 book ai didi

ruby-on-rails - 在 Rails 中使用 add_reference 时如何使用 bigint 类型?

转载 作者:行者123 更新时间:2023-11-29 13:49:56 24 4
gpt4 key购买 nike

在 Rails 5 和 PostgreSQL 中。

我创建这个文件是为了默认使用 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方法?

最佳答案

我找到了答案:

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

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

关于ruby-on-rails - 在 Rails 中使用 add_reference 时如何使用 bigint 类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42828743/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com