pgschema
pgschema copied to clipboard
Inconsistent behaviour with multi-column primary key and partitioning
The following SQL schema appears to produce inconsistent results:
create table some_table(
class_result_id bigint generated by default as identity,
class_run_id integer,
primary key (class_result_id, class_run_id)
) partition by list (class_run_id);
Applying this file
pgschema apply --host localhost --db dbname --user dbuser --password userpass --file testschema.sql
Output:
Plan: 1 to add.
Summary by type:
tables: 1 to add
Tables:
+ some_table
DDL to be executed:
--------------------------------------------------
CREATE TABLE IF NOT EXISTS some_table (
class_result_id bigint GENERATED BY DEFAULT AS IDENTITY,
class_run_id integer,
CONSTRAINT some_table_pkey PRIMARY KEY (class_run_id, class_result_id)
) PARTITION BY LIST (class_run_id);
Do you want to apply these changes? (yes/no): yes
Applying changes...
Executing group 1/1...
Executing 1 statements in implicit transaction
Changes applied successfully!
And generating a plan afterwards results in new changes.
PGPASSWORD=userpass pgschema plan \
--host localhost \
--db dbname \
--user dbuser \
--schema public \
--file testschema.sql \
--output-human stdout \
--output-sql plan.sql
With the following content:
ALTER TABLE some_table DROP CONSTRAINT some_table_pkey;
ALTER TABLE some_table
ADD CONSTRAINT some_table_pkey PRIMARY KEY (class_result_id, class_run_id);
For some reason the pgschema apply command seems to change the order of the primary key elements.