v12.5
I added a deferrable FK constraint on sales_detail to ensure that no one can delete records from sales_header when an associated sales_detail record exists. That works perfectly.
The problem is that try to delete parent records before the child records inside a transaction also fails.
Here are sample tables, sample data and the failed delete statement.
What am I missing?
test=# \d sales_header
Partitioned table "public.sales_header"
Column | Type | Collation | Nullable | Default
---------------+-----------------------------+-----------+----------+---------
order_num | integer | | |
cust_id | integer | | not null |
order_ts | timestamp without time zone | | not null |
shipping_addr | text | | |
Partition key: RANGE (order_ts)
Indexes:
"sales_header_pkey" PRIMARY KEY, btree (cust_id, order_ts)
"sales_header_i1" btree (order_num)
Referenced by:
TABLE "sales_detail" CONSTRAINT "fk_sales_detail_sales_header"
FOREIGN KEY (cust_id, order_ts) REFERENCES sales_header(cust_id, order_ts) DEFERRABLE
test=# \d sales_detail
Partitioned table "public.sales_detail"
Column | Type | Collation | Nullable | Default
--------------+-----------------------------+-----------+----------+---------
cust_id | integer | | not null |
order_ts | timestamp without time zone | | not null |
seq_no | integer | | not null |
inventory_id | integer | | |
quantity | numeric(10,2) | | |
price | numeric(10,2) | | |
tax_rate | numeric(3,3) | | |
Partition key: RANGE (order_ts)
Indexes:
"sales_detail_pkey" PRIMARY KEY, btree (cust_id, order_ts, seq_no)
Foreign-key constraints:
"fk_sales_detail_sales_header" FOREIGN KEY (cust_id, order_ts)
REFERENCES sales_header(cust_id, order_ts) DEFERRABLE
Number of partitions: 12 (Use \d+ to list them.)
INSERT INTO sales_header VALUES (1, 1, '2020-01-05 13:05:42.567', '123 Main St, Anytown, Iowa');
INSERT INTO sales_detail VALUES (1, '2020-01-05 13:05:42.567', 1, 12345, 5.8, 28.40, 0.092);
INSERT INTO sales_detail VALUES (1, '2020-01-05 13:05:42.567', 2, 23456, 6.0, 98.40, 0.092);
INSERT INTO sales_detail VALUES (1, '2020-01-05 13:05:42.567', 3, 34567, 1.8, 67.00, 0.092);
INSERT INTO sales_detail VALUES (1, '2020-01-05 13:05:42.567', 4, 45678, 450, 2.00, 0.092);
INSERT INTO sales_header VALUES (2, 1, '2020-02-05 13:05:42.567', '234 Main St, Anytown, Iowa');
INSERT INTO sales_detail VALUES (1, '2020-02-05 13:05:42.567', 1, 6575, 5.2, 567, 0.045);
INSERT INTO sales_detail VALUES (1, '2020-02-05 13:05:42.567', 2, 4565, 456, 545, 0.045);
INSERT INTO sales_detail VALUES (1, '2020-02-05 13:05:42.567', 3, 7899, 768, 432, 0.045);
INSERT INTO sales_detail VALUES (1, '2020-02-05 13:05:42.567', 4, 2354, 556, 890, 0.045);
INSERT INTO sales_header VALUES (3, 1, '2020-03-05 13:05:42.567', '345 Main St, Anytown, Iowa');
INSERT INTO sales_detail VALUES (1, '2020-03-05 13:05:42.567', 1, 6575, 5.2, 567, 0.045);
INSERT INTO sales_detail VALUES (1, '2020-03-05 13:05:42.567', 2, 4565, 456, 545, 0.045);
INSERT INTO sales_detail VALUES (1, '2020-03-05 13:05:42.567', 3, 7899, 768, 432, 0.045);
test=# begin transaction;
BEGIN
test=# delete from sales_header where cust_id = 1;
ERROR: update or delete on table "sales_header_202001" violates foreign key constraint "sales_detail_cust_id_order_ts_fkey" on table "sales_detail"
DETAIL: Key (cust_id, order_ts)=(1, 2020-01-05 13:05:42.567) is still referenced from table "sales_detail".
test=#
test=# rollback;
--
Angular momentum makes the world go 'round.
Angular momentum makes the world go 'round.