简体   繁体   English

在PostGres中插入NULL值后,如何自动生成ID?

[英]How do I get my IDs auto-generated when a NULL value is inserted in PostGres?

I'm using PostGres 9.5. 我正在使用PostGres 9.5。 I have the following set up in my table, in hopes of auto-generating IDs … 我在表中进行了以下设置,希望自动生成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()

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 我正在像这样调用Rails 4.2.7 activerecord-import gem自动生成SQL。

MyObjectTime.import inserts

How do I get my IDs auto-generated for me if none is supplied? 如果没有提供我的ID,该如何为我自动生成ID?

Try this, 尝试这个,

columns_without_id = YourModel.column_names.reject { |column| column == 'id' }
import(columns_without_id, records)

Ref: Github issue 参考: Github问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM