CREATE SCHEMA issue_7887; SET search_path to 'issue_7887'; CREATE TABLE local1 ( id text not null primary key ); CREATE TABLE reference1 ( id int not null primary key, reference_col1 text not null ); SELECT create_reference_table('reference1'); create_reference_table --------------------------------------------------------------------- (1 row) CREATE TABLE local2 ( id int not null generated always as identity, local1fk text not null, reference1fk int not null, constraint loc1fk foreign key (local1fk) references local1(id), constraint reference1fk foreign key (reference1fk) references reference1(id), constraint testlocpk primary key (id) ); INSERT INTO local1(id) VALUES ('aaaaa'), ('bbbbb'), ('ccccc'); INSERT INTO reference1(id, reference_col1) VALUES (1, 'test'), (2, 'test2'), (3, 'test3'); -- -- Partial insert: omit the identity column -- This triggers the known bug in older code paths if not fixed. -- INSERT INTO local2(local1fk, reference1fk) SELECT id, 1 FROM local1; -- Check inserted rows in local2 SELECT * FROM local2; id | local1fk | reference1fk --------------------------------------------------------------------- 1 | aaaaa | 1 2 | bbbbb | 1 3 | ccccc | 1 (3 rows) -- We do a "INSERT INTO local2(id, local1fk, reference1fk) SELECT 9999, id, 2" which -- should fail under normal PG rules if no OVERRIDING clause is used. INSERT INTO local2(id, local1fk, reference1fk) SELECT 9999, id, 2 FROM local1 LIMIT 1; ERROR: cannot insert a non-DEFAULT value into column "id" DETAIL: Column "id" is an identity column defined as GENERATED ALWAYS. HINT: Use OVERRIDING SYSTEM VALUE to override. -- Using OVERRIDING SYSTEM VALUE to override ALWAYS identity INSERT INTO local2(id, local1fk, reference1fk) OVERRIDING SYSTEM VALUE SELECT 9999, id, 2 FROM local1 LIMIT 1; -- Create a second table with BY DEFAULT identity to test different identity mode CREATE TABLE local2_bydefault ( id int NOT NULL GENERATED BY DEFAULT AS IDENTITY, local1fk text NOT NULL, reference1fk int NOT NULL, CONSTRAINT loc1fk_bd FOREIGN KEY (local1fk) REFERENCES local1(id), CONSTRAINT reference1fk_bd FOREIGN KEY (reference1fk) REFERENCES reference1(id), CONSTRAINT testlocpk_bd PRIMARY KEY (id) ); INSERT INTO local1(id) VALUES ('xxxxx'), ('yyyyy'), ('ddddd'), ('zzzzz'); INSERT INTO local2_bydefault(local1fk, reference1fk) SELECT 'xxxxx', 1; -- Show inserted row in local2_bydefault SELECT * FROM local2_bydefault; id | local1fk | reference1fk --------------------------------------------------------------------- 1 | xxxxx | 1 (1 row) -- -- Overriding a BY DEFAULT identity with user value -- (which is allowed even without OVERRIDING clause). -- -- Provide explicit id for BY DEFAULT identity => no special OVERRIDING needed INSERT INTO local2_bydefault(id, local1fk, reference1fk) VALUES (5000, 'yyyyy', 2); -- Show rows (we expect id=5000 and one with auto-generated ID) SELECT * FROM local2_bydefault ORDER BY id; id | local1fk | reference1fk --------------------------------------------------------------------- 1 | xxxxx | 1 5000 | yyyyy | 2 (2 rows) -- Insert referencing reference1fk=3 => partial insert on both tables INSERT INTO local2(local1fk, reference1fk) VALUES ('ddddd', 3); INSERT INTO local2_bydefault(local1fk, reference1fk) SELECT 'zzzzz', 3; -- Show final state of local2 and local2_bydefault SELECT 'local2' as table_name, * FROM local2 UNION ALL SELECT 'local2_bydefault', * FROM local2_bydefault ORDER BY table_name, id; table_name | id | local1fk | reference1fk --------------------------------------------------------------------- local2 | 1 | aaaaa | 1 local2 | 2 | bbbbb | 1 local2 | 3 | ccccc | 1 local2 | 4 | ddddd | 3 local2 | 9999 | aaaaa | 2 local2_bydefault | 1 | xxxxx | 1 local2_bydefault | 2 | zzzzz | 3 local2_bydefault | 5000 | yyyyy | 2 (8 rows) DROP SCHEMA issue_7887 CASCADE; NOTICE: drop cascades to 8 other objects DETAIL: drop cascades to table local1_102010 drop cascades to table reference1 drop cascades to table reference1_102008 drop cascades to table local2_102009 drop cascades to table local2 drop cascades to table local1 drop cascades to table local2_bydefault_102011 drop cascades to table local2_bydefault