Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

PostgreSQL, héritage.

2 réponses
Avatar
Rue des Prairies
Bonjour,

Soient les tables suivantes (simplifiées)

CREATE TABLE annuaire (
annu_id serial NOT NULL,
annu_category int4 NOT NULL DEFAULT 0,
CONSTRAINT annu_pk1 PRIMARY KEY (annu_id)
) WITH OIDS;

CREATE TABLE users (
annu_id int4 NOT NULL,
annu_login char(20) NOT NULL,
CONSTRAINT annu_un1 UNIQUE (annu_login)
) INHERITS (annuaire);

CREATE TABLE newstheme (
nwth_id serial NOT NULL,
nwth_label1 varchar NOT NULL,
CONSTRAINT nwth_pk1 PRIMARY KEY (nwth_id)
) WITH OIDS;

CREATE TABLE nwannutheme (
nwan_annuid int4 NOT NULL,
nwan_nwthid int4 NOT NULL,

CONSTRAINT nwan_fk1 FOREIGN KEY (nwan_annuid) REFERENCES
annuaire(annu_id) ON DELETE CASCADE ON UPDATE NO ACTION NOT DEFERRABLE
INITIALLY IMMEDIATE,
CONSTRAINT nwan_fk2 FOREIGN KEY (nwan_nwthid) REFERENCES
newstheme(nwth_id) ON DELETE CASCADE ON UPDATE NO ACTION NOT DEFERRABLE
INITIALLY IMMEDIATE,
CONSTRAINT nwan_pk1 PRIMARY KEY (nwan_annuid,nwan_nwthid)
) WITH OIDS;



testdb=# select * from users;
annu_id | annu_category | annu_login
---------+---------------+----------------------
3 | 1 | toto
4 | 4 | titi
(2 rows)

testdb=# select * from annuaire;
annu_id | annu_category
---------+---------------
1 | 1
2 | 2
3 | 1
4 | 4
(4 rows)

testdb=# select * from newstheme;
nwth_id | nwth_label1
---------+-------------
1 | theme1
2 | theme2
(2 rows)

testdb=# select * from nwannutheme;
nwan_annuid | nwan_nwthid | nwan_datemod
-------------+-------------+----------------------------
1 | 1 | 2004-09-15 16:06:32.641742
2 | 1 | 2004-09-15 16:06:39.967742
(2 rows)



Ceci étant posé, voici le problème rencontré, lorsque j'insère dans
nwannutheme un annuid qui est dans annuaire mais pas dans users,
insert into nwannutheme (nwan_annuid,nwan_nwthid) values(2,1);
ça fonctionne,

si annuid est dans users,
insert into nwannutheme (nwan_annuid,nwan_nwthid) values(3,1);
j'ai le message :

ERROR: nwan_fk2 referential integrity violation - key referenced from
nwannutheme not found in annuaire


Question, y a-t-il une solution pour gérer les clefs étrangères d'une
table héritée ?

PostgreSQL 7.4

merci,
Olivier.

2 réponses

Avatar
Nicolas Kowalski
Rue des Prairies writes:

Bonjour,



Bonjour.

[...]

Question, y a-t-il une solution pour gérer les clefs étrangères d'une
table héritée ?



Non, je ne crois pas malheureusement.

À la fin de la page de doc qui va bien
(http://www.postgresql.com/docs/7.4/interactive/ddl-inherit.html), on
peut lire ceci:

"
A limitation of the inheritance feature is that indexes (including
unique constraints) and foreign key constraints only apply to single
tables, not to their inheritance children. Thus, in the above example,
specifying that another table's column REFERENCES cities(name) would
allow the other table to contain city names but not capital
names. This deficiency will probably be fixed in some future release.
"


--
Nicolas
Avatar
Rue des Prairies
Nicolas Kowalski a écrit :

Rue des Prairies writes:


Bonjour,




Bonjour.

[...]


Question, y a-t-il une solution pour gérer les clefs étrangères d'une
table héritée ?




Non, je ne crois pas malheureusement.

À la fin de la page de doc qui va bien
(http://www.postgresql.com/docs/7.4/interactive/ddl-inherit.html), on
peut lire ceci:

"
A limitation of the inheritance feature is that indexes (including
unique constraints) and foreign key constraints only apply to single
tables, not to their inheritance children. Thus, in the above example,
specifying that another table's column REFERENCES cities(name) would
allow the other table to contain city names but not capital
names. This deficiency will probably be fixed in some future release.
"





Too bad, merci néanmoins pour ta réponse

Olivier.