postgresql - Creating rule for update view sql error -
i trying create updaetable view.
i have table product, category, product_has_category.
create table "category" ( "category_id" serial not null, "name" varchar(15) not null, "description" varchar(150) not null, primary key("category_id") ); create table "product" ( "product_id" serial not null, "name" varchar(20) not null, "price" int4 not null, "description" varchar(200), "country_of_origin" varchar(20) not null, primary key("product_id") ); create table "product_has_category" ( "nmid" serial not null, "product_id" int4 not null, "category_id" int4 not null ); alter table "product_has_category" add constraint "ref_product_has_category_to_product" foreign key ("product_product_id") references "product"("product_id") match simple on delete cascade on update cascade not deferrable; and here view selects products, price , categories:
create view products_with_categories select product.name product_name, product.price, category.name category product, category, product_has_category product_has_category.product_id = product.product_id , product_has_category.category_id = category.category_id order product_name; i want view updatable , created rule:
create rule prod_cat_upd on update products_with_categories instead update product set product.name=new.product.name product.name=old.product.name and got following error:
invalid reference from-clause entry table "product" hint: there entry table "product", cannot referenced part of query.
i can not understand error means , how solve problem.
try syntax create rule, refer more documentation
create rule prod_cat_upd on update products_with_categories instead update product set name=new.name name=old.name i created 2 sql fiddle sample table
1) sample sql fiddle solution, provide :- working fine
create table shoelace_data ( sl_name text, -- primary key sl_avail integer, -- available number of pairs sl_color text, -- shoelace color sl_len real, -- shoelace length sl_unit text -- length unit ); create table unit ( un_name text, -- primary key un_fact real -- factor transform cm ); create view shoelace select s.sl_name, s.sl_avail, s.sl_color, s.sl_len, s.sl_unit, s.sl_len * u.un_fact sl_len_cm shoelace_data s, unit u s.sl_unit = u.un_name; create rule shoelace_upd on update shoelace instead update shoelace_data set sl_name = new.sl_name, sl_avail = new.sl_avail, sl_color = new.sl_color, sl_len = new.sl_len, sl_unit = new.sl_unit sl_name = old.sl_name; 2) code have create rule :- failed , have same error get
not working
create rule shoelace_upd on update shoelace instead update shoelace_data set shoelace_data.sl_name = new.shoelace_data.sl_name, shoelace_data.sl_avail = new.shoelace_data.sl_avail, shoelace_data.sl_color = new.shoelace_data.sl_color, shoelace_data.sl_len = new.shoelace_data.sl_len, shoelace_data.sl_unit = new.shoelace_data.sl_unit shoelace_data.sl_name = old.shoelace_data.sl_name; edit:- getting error:-
error: column new.name not exist
because view not have 'name' field, view have these 3 fields :- product_name, price , category use these fields only
create rule prod_cat_upd on update products_with_categories instead update product set name=new.product_name name=old.product_name
Comments
Post a Comment