I’m using PostGres 9.5. I have the following set up in my table, in hopes of auto-generating IDs …
myproject=> \d my_object_times
Table "public.my_object_times"
Column | Type | Modifiers
-------------------+-----------------------------+-------------------------------------
…
time_in_ms | bigint |
created_at | timestamp without time zone | not null
updated_at | timestamp without time zone | not null
name | character varying |
age | integer |
city | character varying |
state_id | integer |
country_id | integer |
overall_rank | integer |
age_group_rank | integer |
gender_rank | integer |
races_id | integer |
event_id | character varying |
id | character varying | not null default uuid_generate_v4()
But when I try and run a bulk insert statement, the content of which looks something like
INSERT INTO "my_object_times" ("first_name","last_name","time_in_ms","created_at","updated_at","name","participant","age","city","state_id","country_id","overall_rank","age_group_rank","gender_rank","races_id","event_id","id","division_low_age","division_high_age") VALUES (NULL,NULL,1403000,'2016-10-12 15:36:42.766936','2016-10-12 15:36:42.767104','Terry Williamson',NULL,NULL,NULL,NULL,NULL,4,1,NULL,NULL,'0bf8c3bc-3577-4cab-bdd4-61e64655eaed',NULL,3,3),(NULL,NULL,1431000,'2016-10-12 15:36:42.766936','2016-10-12 15:36:42.767104','Casey Reinl',NULL,NULL,NULL,NULL,NULL,5,1,NULL,NULL,'0bf8c3bc-3577-4cab-bdd4-61e64655eaed',NULL,2,2),(NULL,NULL,1473000,'2016-10-12 15:36:42.766936
I get an error
Error during processing: PG::NotNullViolation: ERROR: null value in column "id" violates not-null constraint
DETAIL: Failing row contains (null, null, 1403000, 2016-10-12 15:36:42.766936, 2016-10-12 15:36:42.767104, Terry Williamson, null, null, null, null, null, 4, 1, null, null, 0bf8c3bc-3577-4cab-bdd4-61e64655eaed, null, 3, 3).
The SQL is being auto generated by the Rails 4.2.7 activerecord-import gem, which I’m invoking like so
MyObjectTime.import inserts
How do I get my IDs auto-generated for me if none is supplied?
Try this,
columns_without_id = YourModel.column_names.reject { |column| column == 'id' }
import(columns_without_id, records)
Ref: Github issue