Thread: REFERENCES ignored when there is inheritance?

REFERENCES ignored when there is inheritance?

From
Stephane Bortzmeyer
Date:
I have a table to store localization information:

CREATE TABLE Localization (
...
 zipcode TEXT NOT NULL,
...
 country INTEGER REFERENCES Countries (id) NOT NULL

The table Countries is like:

CREATE TABLE Countries (
 id SERIAL UNIQUE,
 name TEXT UNIQUE NOT NULL,
 code CHAR(2) UNIQUE NOT NULL);

And a table Contacts_short which inherits from it so all the Contacts
have localization information:

CREATE TABLE Contacts_short (
...
INHERITS (Localization, Objects) WITHOUT OIDS;

I have 240 countries in the database. Because of a programming error,
contacts were entered with a country > 240. I thought that the
"REFERENCES Countries (id)" should have prevented it. Is it because of
inheritance?

PostgreSQL 7.4.7

Example:

registry=> SELECT max(id) FROM Countries;
 max
-----
 240
(1 row)

registry=> SELECT count(*) FROM Contacts_short WHERE country > 240;
 count
-------
    84
(1 row)

To me, the last figure should have been zero.

Re: REFERENCES ignored when there is inheritance?

From
Stephane Bortzmeyer
Date:
On Fri, May 13, 2005 at 11:57:08AM +0200,
 Stephane Bortzmeyer <bortzmeyer@nic.fr> wrote
 a message of 49 lines which said:

> I have 240 countries in the database. Because of a programming error,
> contacts were entered with a country > 240. I thought that the
> "REFERENCES Countries (id)" should have prevented it. Is it because of
> inheritance?

Yes, it is even documented :-(

http://www.postgresql.org/docs/7.4/interactive/ddl-inherit.html

I agree with the comments at the end. Having, as a workaround, to add
a FOREIGN KEY REFERENCES to every child table defeats a lot of the
purpose of inheritance.

It does not seem to be better in 8.0:

http://www.postgresql.org/docs/8.0/interactive/ddl-inherit.html