Re: Deferrable FK not behaving as expected.

From: Ron <ronljohnsonjr(at)gmail(dot)com>
To: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: Deferrable FK not behaving as expected.
Date: 2020-12-07 18:30:48
Message-ID: 3c32cc9c-1589-7f22-b9e3-f4bcccbaf87e@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 12/7/20 12:19 PM, Thomas Kellerer wrote:
> Ron schrieb am 07.12.2020 um 19:15:
>> 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*
>
> I think if you only mention "deferrable" this is the same as "deferrable
> initially immediate",
> so you will need to make them deferrable in your transaction:
>
>     SET CONSTRAINTS ALL DEFERRED;
>
> or create the constraint with
>
>     deferrable initially deferred

Neither technique worked.

test=# ALTER TABLE sales_detail ALTER CONSTRAINT
fk_sales_detail_sales_header DEFERRABLE INITIALLY DEFERRED;
ALTER TABLE

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 INITIALLY
DEFERRED*
Number of partitions: 12 (Use \d+ to list them.)

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=# rollback;
ROLLBACK

--
Angular momentum makes the world go 'round.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Joshua Drake 2020-12-07 18:35:39 Re: PL/java
Previous Message Ron 2020-12-07 18:26:26 Re: Deferrable FK not behaving as expected.