Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
789 views
in Technique[技术] by (71.8m points)

postgresql - Postgres error: null value in column “id” - during SQL insert operation with UUID

I did change the structure of my tables to use UUID as ID instead of Integer or Serial. I had some data which I did export from the old structure and and I wish to import those into the one. But I get a "null value in column "id" violates not-null constraint" error when I try to run:

INSERT INTO "public"."locations" ("name","description","location_id","parent_id","parent_path","create_uid","create_date","write_uid","write_date") 
VALUES 
('Kitchen', NULL, NULL, NULL, 'root', NULL, NULL, NULL, NULL),
('Fridge', NULL, NULL, 1, 'root.1', NULL, NULL, NULL, NULL), .....

Despite of having a working random id (UUID) value by default.

  "id" varchar(50) COLLATE "pg_catalog"."default" DEFAULT gen_random_uuid(),

I don't know much about the different ways to create the UUID, so maybe there is a better one, which doesn't cause the error. The postgres version is PostgreSQL 12.

Here the locations table infos:
Table "public.locations"

   Column    |              Type              | Collation | Nullable |                   Default                   | Storage  | Stats target | Description 
-------------+--------------------------------+-----------+----------+---------------------------------------------+----------+--------------+-------------
 id          | character varying(50)          |           | not null | gen_random_uuid()                           | extended |              | 
 name        | character varying(55)          |           | not null |                                             | extended |              | 
 description | text                           |           |          |                                             | extended |              | 
 sequence    | integer                        |           | not null | nextval('locations_sequence_seq'::regclass) | plain    |              | 
 location_id | character varying(50)          |           |          |                                             | extended |              | 
 parent_id   | character varying(50)          |           |          |                                             | extended |              | 
 parent_path | ltree                          |           |          |                                             | extended |              | 
 create_uid  | character varying(50)          |           |          |                                             | extended |              | 
 create_date | timestamp(6) without time zone |           |          |                                             | plain    |              | 
 write_uid   | character varying(50)          |           |          |                                             | extended |              | 
 write_date  | timestamp(6) without time zone |           |          |                                             | plain    |              | 
Indexes:
    "locations_pkey" PRIMARY KEY, btree (id)
    "locations_name" UNIQUE CONSTRAINT, btree (name) WITH (fillfactor='10')
    "locations_parent_path_idx" gist (parent_path)
Foreign-key constraints:
    "locations_create_uid_fkey" FOREIGN KEY (create_uid) REFERENCES res_users(id) ON DELETE SET NULL
    "locations_parent_id_fkey" FOREIGN KEY (parent_id) REFERENCES locations(id)
    "locations_write_uid_fkey" FOREIGN KEY (write_uid) REFERENCES res_users(id) ON DELETE SET NULL
Referenced by:
    TABLE "products" CONSTRAINT "location_id_fkey" FOREIGN KEY (location_id) REFERENCES locations(id)
    TABLE "locations" CONSTRAINT "locations_parent_id_fkey" FOREIGN KEY (parent_id) REFERENCES locations(id)
Access method: heap

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...