Обсуждение: BUG #4327: Primary key not refresh after cascaded deleted

Поиск
Список
Период
Сортировка

BUG #4327: Primary key not refresh after cascaded deleted

От
"Sam Wong"
Дата:
The following bug has been logged online:

Bug reference:      4327
Logged by:          Sam Wong
Email address:      sam@hellosam.net
PostgreSQL version: 8.3.1
Operating system:   Linux Debian
Description:        Primary key not refresh after cascaded deleted
Details:

Say, I have two table A and B.

That A has one column:
A_primary_col
...and keys:
PRIMARY KEY (A_primary_col)

That B has two columns:
B_primary_col, A_reference
...and keys:
PRIMARY KEY (b_primary_col)
FOREIGN KEY (A_reference) REFERENCES A(A_primary_col) ON DELETE CASCADE

Now I inserted the following records into
into A:
1
2
3

into B:
A,1
B,2
C,3

Now I delete all records from A:
DELETE FROM A;
so that both table is empty now.

Now when I try to insert the following:
into A:
4

Then when I try to insert the following into B:
A,4

Expected Behavior:
it will insert with no problem

Actual Behavior:
it will say
duplicate key value violates unique constraint "B_primary_column_pkey".

Workaround:
Analyze the table B.

I believe that's a bug that the primary key index is not updated
accordingly? Thanks.

Re: BUG #4327: Primary key not refresh after cascaded deleted

От
Tom Lane
Дата:
"Sam Wong" <sam@hellosam.net> writes:
> Then when I try to insert the following into B:
> A,4
> it will say
> duplicate key value violates unique constraint "B_primary_column_pkey".

Works for me:

regression=# create table a (A_primary_col int primary key);
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "a_pkey" for table "a"
CREATE TABLE
regression=# insert into a values (1),(2),(3);
INSERT 0 3
regression=# create table b (B_primary_col text primary key, a_reference int references a on delete cascade);
NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "b_pkey" for table "b"
CREATE TABLE
regression=# insert into b values('A',1),('B',2),('C',3);
INSERT 0 3
regression=# DELETE FROM A;
DELETE 3
regression=# select * from a;
 a_primary_col
---------------
(0 rows)

regression=# select * from b;
 b_primary_col | a_reference
---------------+-------------
(0 rows)

regression=# insert into a values (4);
INSERT 0 1
regression=# insert into b values('A',4);
INSERT 0 1

I suspect a mistake on your part.  If you can actually reproduce this
problem, please show an exact test case, not hand-waving.

            regards, tom lane