当在 PostGres 中插入 NULL 值时,如何让我的 ID 自动生成?
How do I get my IDs auto-generated when a NULL value is inserted in PostGres?
我正在使用 PostGres 9.5。我在 table 中设置了以下内容,希望能自动生成 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
如果提供 none,我如何让我的 ID 自动生成?
试试这个,
columns_without_id = YourModel.column_names.reject { |column| column == 'id' }
import(columns_without_id, records)
参考:Github issue
我正在使用 PostGres 9.5。我在 table 中设置了以下内容,希望能自动生成 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
如果提供 none,我如何让我的 ID 自动生成?
试试这个,
columns_without_id = YourModel.column_names.reject { |column| column == 'id' }
import(columns_without_id, records)
参考:Github issue