【发布时间】:2010-05-31 09:01:47
【问题描述】:
我正在尝试在 postgresql 上开发一个 Rails 应用程序,使用序列来增加字段,而不是基于 validates_uniqueness_of 的默认 ruby 方法。
事实证明,这具有挑战性,原因有很多: 1.这是对现有表的迁移,不是新表或新列 2. 使用参数 :default => "nextval('seq')" 不起作用,因为它试图在括号中设置它 3. 最终分两步完成迁移:
change_column :work_commencement_orders, :wco_number_suffix, :integer, :null => false#, :options => "set default nextval('wco_number_suffix_seq')"
execute %{
ALTER TABLE work_commencement_orders ALTER COLUMN wco_number_suffix SET DEFAULT nextval('wco_number_suffix_seq');
}
现在这似乎在开发数据库中做了正确的事情,架构看起来像:
wco_number_suffix | integer | not null default nextval('wco_number_suffix_seq'::regclass)
但是,测试失败了
PGError: ERROR: null value in column "wco_number_suffix" violates not-null constraint
: INSERT INTO "work_commencement_orders" ("expense_account_id", "created_at",
"process_id", "vo2_issued_on", "wco_template", "updated_at", "notes", "process_type",
"vo_number", "vo_issued_on", "vo2_number", "wco_type_id", "created_by",
"contractor_id", "old_wco_type", "master_wco_number", "deadline", "updated_by",
"detail", "elective_id", "authorization_batch_id", "delivery_lat", "delivery_long",
"operational", "state", "issued_on", "delivery_detail") VALUES(226, '2010-05-31
07:02:16.764215', 728, NULL, E'Default', '2010-05-31 07:02:16.764215', NULL,
E'Procurement::Process', NULL, NULL, NULL, 226, NULL, 276, NULL, E'MWCO-213',
'2010-06-14 07:02:16.756952', NULL, E'Name 4597', 220, NULL, NULL, NULL, 'f',
E'pending', NULL, E'728 Test Road; Test Town; 1234; Test Land') RETURNING "id"
当你检查测试数据库的架构时可以找到解释:
wco_number_suffix | integer | not null
那么默认值发生了什么?
我尝试添加
task:
template: smmt_ops_development
到具有发布效果的database.yml文件
create database smmt_ops_test template = "smmt_ops_development" encoding = 'utf8'
我已经验证,如果我发出这个,那么它实际上会复制默认的 nextval。很明显,rails 在那之后又做了一些事情来压制它。
关于如何解决此问题的任何建议?
谢谢 罗伯特
【问题讨论】:
标签: ruby-on-rails postgresql activerecord templates sequence