ERROR: unique constraint on partitioned table must include all partitioning columns

PostgreSQL Version

partdb=> select version();
                                                 version                                                 
---------------------------------------------------------------------------------------------------------
 PostgreSQL 14.2 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit
(1 row)

Problem:

建立Partition Table,欄位有Primary key時,會出現Error

partdb=> create table part_table(id int primary key,
name varchar,
create_time timestamp without time zone not null default clock_timestamp() )
partition by range (create_time);

ERROR:  unique constraint on partitioned table must include all partitioning columns
DETAIL:  PRIMARY KEY constraint on table "part_table" lacks column "create_time" which is part of the partition key.

Solution:

Partition切割的欄位跟Primary key欄位合併成Primary key

partdb=> create table part_table(id int, 
name varchar, 
create_time timestamp without time zone not null default clock_timestamp(), 
constraint part_test_pk primary key (id, create_time) ) 
partition by range (create_time);

CREATE TABLE

張貼留言

0 留言