I am trying to develop a rails application on postgresql using a sequence to increase the field instead of the standard ruby ββapproach based on validates_uniqueness_of.
This turned out to be difficult for several reasons: 1. This is a transfer of an existing table, not a new table or column 2. Using the parameter: default => "nextval (" seq ")" does not work, because it tries to set it in brackets 3. In As a result, migration appeared, working in 2 stages:
change_column :work_commencement_orders, :wco_number_suffix, :integer, :null => false
Now this seems to have done the right thing in the development database, and the schema looks like this:
wco_number_suffix | integer | not null default nextval('wco_number_suffix_seq'::regclass)
However, tests do not work with
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"
An explanation can be found when checking the test database schema:
wco_number_suffix | integer | not null
So what happened with the default?
I tried adding
task: template: smmt_ops_development
to the database.yml file, which has the effect of issuing
create database smmt_ops_test template = "smmt_ops_development" encoding = 'utf8'
I checked that if I do this, it will actually copy the nextval value by default. Therefore, it is clear that the rails do something after this in order to crush it again.
Any suggestions on how to fix this?
Thanks Robert