Trigger de gestion des stock.

Fermé
Rahan - 29 avril 2010 à 15:19
 Rahan - 4 mai 2010 à 11:53
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:
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;



Une table commande:

 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


Et une table permettant le lien entre commande et produit:

 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;


Aprés plusieurs heures de recherche sur le net et d'essai, je n'ai pas réussi à concevoir un trigger et fonction associé qui permettrait 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. Tout ce que j'ai trouvé était à executer sous oracle ou mysql serveur:
http://www.sqlfr.com/codes/TRIGGER-MAJ_48866.aspx

Voilà ou j'en suis actuellement:


 DROP FUNCTION gest_stock()cascade;
CREATE OR REPLACE FUNCTION gest_stock()
  RETURNS opaque AS
$BODY$
    declare stock varchar;
    declare    id_prod integer;
    declare    q integer;
    begin
        select into q new.quantity
        where 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 
        where id_prod = 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;

CREATE TRIGGER gestion_stock
  after INSERT or delete or update
  ON t_order_product
  FOR EACH ROW
  EXECUTE PROCEDURE gest_stock();


Ca ne marche bien évidemment pas.

Merci d'ores et déjà pour toute l'aide et les réponses que vous pourrez m'apporter.

2 réponses

Aprés avoir pris en compte certaines corrections mon programme me renvoi l'erreur suivante lorsque j'essaye de rentrer une nouvelle ligne dans ma table t_order_product à savoir:


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".


Voilà le trigger corrigé:

</code>
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;</code>

Mais j'ai lu ici http://blog.guillaume.lelarge.info/inde ... g%C3%A8res , si j'ai bien compris, qu'il y avait un problème sous postgres avec des trigger qui désactivé les clés étrangères.

Est-ce que ce que je cherche à faire est impossible?
La version de postgreSQL que j'utilise: 8.4

Julien
1
Un petit up! Quelqu'un a-t-il déjà rencontré ce problème?
1