a rule question - Mailing list pgsql-novice

From Brad Paul
Subject a rule question
Date
Msg-id 0c03e07331716a2FE6@mail6.carolina.rr.com
Whole thread Raw
Responses Re: a rule question  (Ludwig Lim <lud_nowhere_man@yahoo.com>)
List pgsql-novice
I have two tables one is called "inventory" and the other is
"inventory_usage". When I insert into inventory_usage info like number of
widgets used for a given order_number I would the in_stock entry in the
inventory to be adjusted. Here are the two tables and my current attempts at
the rule:

create sequence "inventory_id_seq" start 1 increment 1;
create table "inventory" (
       "inventory_id" int4 default nextval('inventory_id_seq') NOT NULL
unique,
       "in_stock" int4 default 0,
       "name" character varying not null,
       "description" character varying not null,
       "cost_per_unit" money,
       "vendor_code" character varying,
       "vendor_name"  character varying,
       "vendor_address_id" int4   references address (address_id),
       primary key ("name")
);
grant update on inventory  to flip;
grant select on inventory  to flip;

create sequence "inventory_usage_id_seq" start 1 increment 1;
create table "inventory_usage" (
       "inventory_usage_id" int4 default nextval('inventory_usage_id_seq')
NOT NULL unique,
       "inventory_id" int4   references inventory (inventory_id),
       "timestamp" timestamp default now(),
       "used" int4 not null,
       "printer" character varying,
       "real_lpi" real,
       "order_num" int4,
       primary key ("inventory_usage_id")
);
grant all on inventory_usage to flip;
grant all on inventory_usage_id_seq to flip;


create rule inventory_usage_insert_rule as
       on insert to inventory_usage
       do
    update inventory set inventory.in_stock=(select in_stock from inventory
where
inventory_usage.inventory_id=inventory.inventory_id)-inventory_usage.used
           where inventory.inventory_id=inventory_usage.inventory_id;


I have also tried:

create rule inventory_usage_insert_rule as
       on insert to inventory_usage
       do
    update inventory set new.in_stock=old.in_stock-inventory_usage.used
           where inventory.inventory_id=inventory_usage.inventory_id;

I think the second one would work if I put a in_stock column in the
inventory_usage table. But this does not seam like a slick solution.


Thank you
Brad Paul

pgsql-novice by date:

Previous
From: "Josh Berkus"
Date:
Subject: Re: Big Picture
Next
From: "ken"
Date:
Subject: Re: Big Picture