pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Ron <ronljohnsonjr@gmail.com>
To: pgsql-general <pgsql-general@postgresql.org>
Subject: Deferrable FK not behaving as expected.
Date: Mon, 7 Dec 2020 12:15:20 -0600
Message-ID: <75fe0761-a291-86a9-c8d8-4906da077469@gmail.com> (raw)


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.

view thread (10+ messages)  latest in thread

Message-ID: <75fe0761-a291-86a9-c8d8-4906da077469@gmail.com>
Permalink:  ../75fe0761-a291-86a9-c8d8-4906da077469@gmail.com/
Also on:    postgresql.org/message-id/75fe0761-a291-86a9-c8d8-4906da077469@gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: ronljohnsonjr@gmail.com
  Subject: Re: Deferrable FK not behaving as expected.
  In-Reply-To: <75fe0761-a291-86a9-c8d8-4906da077469@gmail.com>
  References: <75fe0761-a291-86a9-c8d8-4906da077469@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox