我正在使用PostGres 9.5。我在表格中设置了以下内容,希望自动生成ID ...
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()
但是当我尝试运行批量插入语句时,其内容看起来像
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
我收到错误
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).
SQL是由Rails 4.2.7 activerecord-import gem自动生成的,我正在这样调用
MyObjectTime.import inserts
如果没有提供我的ID,如何为我自动生成ID?
答案 0 :(得分:0)
试试这个,
columns_without_id = YourModel.column_names.reject { |column| column == 'id' }
import(columns_without_id, records)
参考:Github issue