I am trying to 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 is view that selects products, its price and categories:
CREATE VIEW products_with_categories AS
SELECT product.name AS product_name, product.price, category.name AS category
FROM product, category, product_has_category
WHERE product_has_category.product_id = product.product_id AND
product_has_category.category_id = category.category_id
ORDER BY product_name;
I want view to be updatable and created the rule:
CREATE RULE prod_cat_upd AS ON UPDATE TO products_with_categories
DO INSTEAD
UPDATE product
SET product.name=NEW.product.name
WHERE product.name=OLD.product.name
And I got the following error:
invalid reference to FROM-clause entry for table "product" Hint: There is an entry for table "product", but it cannot be referenced from this part of the query.
I can not understand what does this error means and how to solve this problem.