Foreign keys, table inheritance, and TRUNCATE

From: Florian Weimer <fweimer(at)bfk(dot)de>
To: pgsql-general(at)postgresql(dot)org
Subject: Foreign keys, table inheritance, and TRUNCATE
Date: 2007-01-30 12:33:36
Message-ID: 82wt34zobj.fsf@mid.bfk.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Here's something I've just noticed:

CREATE TABLE foo (f INTEGER PRIMARY KEY);
INSERT INTO foo VALUES (1);
CREATE TABLE bar (b INTEGER REFERENCES foo);
CREATE TABLE bar1 () INHERITS (bar);
INSERT INTO bar1 VALUES (1);

This is quite correct:

TRUNCATE foo;
ERROR: cannot truncate a table referenced in a foreign key constraint
DETAIL: Table "bar" references "foo".
HINT: Truncate table "bar" at the same time, or use TRUNCATE ... CASCADE.

But:

TRUNCATE foo, bar;
SELECT * FROM bar;
b
---
1
(1 row)

SELECT * FROM foo;
f
---
(0 rows)

Whoops. The referential constraint has been violated. Perhaps it's a
good idea to extend TRUNCATE on a parent table to all children?

--
Florian Weimer <fweimer(at)bfk(dot)de>
BFK edv-consulting GmbH http://www.bfk.de/
Kriegsstraße 100 tel: +49-721-96201-1
D-76133 Karlsruhe fax: +49-721-96201-99

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Ron Johnson 2007-01-30 14:10:01 Re: PG Email Client
Previous Message cedric 2007-01-30 11:30:05 Re: Retrieving PK of inserted row