假设我有一个叫Transaction有:transaction_code属性的模型.我希望该属性自动填充可能不同的序列号id(例如,id=1可能有的事务transaction_code=1000).
我试图在postgres上创建一个序列,然后为该序列的transaction_code列创建默认值nextval.问题是,如果我没有@transaction.transaction_code在RoR上分配任何值,当我@transaction.save在RoR上发出一个时,它会尝试执行以下SQL:
INSERT INTO transactions (transaction_code) VALUES (NULL);
这样做是在Transactions表上创建一个新行,其中transaction_code为NULL,而不是计算序列的nextval并将其插入相应的列.因此,正如我所知,如果你为postgres指定NULL,它假定你真的想要在该列中插入NULL,无论它是否具有默认值(我来自ORACLE,它具有不同的行为).
无论是在数据库上还是在RoR上,我都对此有任何解决方案:
save提前致谢.
我正在开发一个Ruby on Rails应用程序.我们正在使用PostgreSQL数据库.
有一个以scores下列列命名的表:
Column | Type
--------------+-----------------------
id | integer
value | double precision
ran_at | timestamp
active | boolean
build_id | bigint
metric_id | integer
platform_id | integer
mode_id | integer
machine_id | integer
higher_better | boolean
job_id | integer
variation_id | integer
step | character varying(255)
Run Code Online (Sandbox Code Playgroud)
我需要一个添加序列来job_id(注:没有模型job).
如何创建此序列?
postgresql activerecord ruby-on-rails rails-postgresql rails-activerecord