我不能像使用Rails5时那样在模型的id中使用bigint,因为我使用的是Rails4.1
我希望我的模型有一个自动递增的id作为正常,但我希望它是一个大整数,而不是一个规则的整数。
transaction表将包含数百万条记录,稍后迁移ids将是一个令人头疼的问题,需要停机。
我首先尝试使用rails生成器(就像一个优秀的懒惰开发人员)
这给了我
class CreateTransactions < ActiveRecord::Migration
def change
create_table :transactions do |t|
t.bigint :id
#more stuff omitted
end
end
end迁移失败的
undefined method `bigint' for #<ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::TableDefinition:0x00007fe8df5c9b88>我已经试着把它改成
create_table :transactions, id: false do |t|
t.bigint :id
end这是可行的。迁移运行,但随后无法自动递增id。意思是,我不能做Transaction.create
或者它从db层对我大喊id上的null约束。
显然,您可以创建一个表,然后用change_column修改它,但这在schema.rb中没有反映,所以我很担心。
显然,^^ (id上的change_column)是一个不可逆的迁移,所以我想避免这种情况。
我知道一定有一种简单的,很好的,很好的方法来实现这一点。
预期结果:
Transaction.create
给我一个新的事务,其中id将自己设置为正常的,但它是一个大的int 8 bits,而不是一个普通的int 4 bits。
发布于 2019-06-21 09:04:35
我只是通过阅读postgres支持的数据类型并猜测rails将如何处理它而弄明白的。
https://www.postgresql.org/docs/10/datatype-numeric.html
create_table :transactions, id: :bigserial do |t|
#other model stuff here
end通过直接连接postgres进行验证
Table "public.transactions"
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
---------------+-----------------------------+-----------+----------+-----------------------------------------------------+----------+--------------+-------------
id | bigint | | not null | nextval('ghost_card_transactions_id_seq'::regclass) | plain | |https://stackoverflow.com/questions/56695577
复制相似问题