Bonjour,
Je suis plus ou moins débutant sur postgres. Je conçois actuellement une base de données pour la gestion de stock.
J'ai donc créer une table produit:
Une table commande:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 CREATE TABLE t_product ( id_product bigserial NOT NULL, code character varying(4), "name" character varying(50) NOT NULL, description character varying(250) NOT NULL, price_ht double precision NOT NULL, stock_web integer NOT NULL, picture_id integer, category_id integer NOT NULL, CONSTRAINT id_product PRIMARY KEY (id_product), CONSTRAINT category_idep FOREIGN KEY (category_id) REFERENCES t_category (id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION, CONSTRAINT picture_idp FOREIGN KEY (picture_id) REFERENCES t_picture (id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION ) WITH ( OIDS=FALSE ); ALTER TABLE t_product OWNER TO postgres;
Et une table permettant le lien entre commande et produit:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 CREATE TABLE t_order ( id serial NOT NULL, create_date date NOT NULL, price_ht double precision NOT NULL, user_id integer NOT NULL, CONSTRAINT order_id PRIMARY KEY (id), CONSTRAINT user_ido FOREIGN KEY (user_id) REFERENCES t_user (id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION ) WITH ( OIDS=FALSE
Aprés plusieurs heures de recherche sur le net et d'essai, jai réussi à concevoir un trigger et fonction associée qui permettent de mettre à jour la champs stock_web de la table t_product à partir de la quantité commandée, renseignée dans le champ quantity de la table t_order_product.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 CREATE TABLE t_order_product ( id serial NOT NULL, product_id integer NOT NULL, quantity integer, price_ht double precision NOT NULL, order_id integer NOT NULL, CONSTRAINT order_product_id PRIMARY KEY (id), CONSTRAINT order_idop FOREIGN KEY (order_id) REFERENCES t_order (id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION, CONSTRAINT product_idop FOREIGN KEY (product_id) REFERENCES t_product (id_product) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE NO ACTION ) WITH ( OIDS=FALSE ); ALTER TABLE t_order_product OWNER TO postgres;
Voici mon trigger:
Mon programme me renvoi cette erreur lorsque j'essaye de rentrer une nouvelle ligne dans ma table t_order_product:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 CREATE OR REPLACE FUNCTION gest_stock() RETURNS trigger AS $BODY$ declare stock varchar; declare id_prod integer; declare q integer; begin select into q new.quantity; select into id_prod new.product_id; update t_product set stock_web = stock_web + q where id_product=id_prod; q :=0; select into q old.quantity; select into id_prod old.product_id; update t_product set stock_web = stock_web - q where id_product=id_prod; return null; end; $BODY$ LANGUAGE 'plpgsql' VOLATILE COST 100; ALTER FUNCTION gest_stock() OWNER TO postgres;
Cependant il existe bien une ligne pour laquelle l'id_product (qui correspond au champ product_id en tant que clé étrangère dans la table t_order_product) est égal à 2 dans la table t_product. Voilà pourquoi je ne comprends pas l'erreur.ERREUR: une instruction insert ou update sur la table "t_order_product" viole la contrainte de clé étrangère "product_idop"
DETAIL: La clé (product_id)= (2) n'est pas présente dans la table "t_product".
Et, très interessant si je modifie mon trigger le passant de "after insert or update..." à "before insert or...".
Il ne me renvoi pas d'erreur mais bien évidemment il ne conserve pas la ligne rentrée dans la table t_order_product" puisque la fonction associée au trigger comprend un "return null", si je met "return new", l'erreur est a nouveau renvoyée.
Dernière information: il n'y a pas d'autres triggers sur la table t_product.
La version de postgreSQL que j'utilise: 8.4.
Voilà, merci de m'avoir lu jusqu'au bout. Pourriez vous m'aider?
Julien
Partager