PostgreSQL触发错误:“ inventory_product”关系的“ t列”不存在”

时间:2019-09-02 19:15:04

标签: sql postgresql database-trigger

我正在尝试创建一个触发器,该触发器根据插入到var index = [0,0,0]; var direction = [1,1,1]; $(".button").on("click", function () { var $this = $(this); if (index[$this.index()] >= $this.find(".letter").length - 1) { direction[$this.index()] *= -1; } else if (direction[$this.index()] == -1 && index[$this.index()] == 0) { direction[$this.index()] *= -1; } $('.letter').each(function(){ $(this).removeClass('highlighted'); }) $(this).children(".letter").eq(index[$this.index()]).addClass('highlighted'); var result = $(this).children(".letter").eq(index[$this.index()]).text(); $(".result").text(result); //you have to add to the index after the result index[$this.index()] += direction[$this.index()]; }); 中的qty来减少inventory_product表上的qty列。触发器可以在MySQL(使用不同的语法)下正常工作,但是我不确定PostgreSQL版本有什么问题。

account_sale上执行插入操作时,我得到:

  

错误:关系“库存产品”的列“ t”不存在

触发器:

inventory_sale

库存产品:

CREATE OR REPLACE FUNCTION update_inventory()
RETURNS TRIGGER AS $$
BEGIN
  IF NEW.product_id IS NOT NULL THEN
    UPDATE inventory_product AS t
    SET t.qty = t.qty - NEW.qty #error is thrown here
    WHERE t.id = NEW.product_id;
  END IF;
END; $$ LANGUAGE 'plpgsql';

CREATE TRIGGER after_insert_account_sale
AFTER INSERT ON account_sale
FOR EACH ROW
EXECUTE PROCEDURE update_inventory();

帐户销售:

CREATE TABLE public.inventory_product
(
    id integer NOT NULL DEFAULT nextval('inventory_product_id_seq'::regclass),
    upc character varying(45) COLLATE pg_catalog."default",
    sku character varying(45) COLLATE pg_catalog."default",
    asin character varying(45) COLLATE pg_catalog."default",
    ebay_sku character varying(45) COLLATE pg_catalog."default",
    tcgplayer_sku integer,
    qty integer NOT NULL,
    opt_qty integer NOT NULL,
    reserve integer NOT NULL,
    sell_price numeric(10,2) NOT NULL,
    buy_price numeric(10,2) NOT NULL,
    product_weight_g numeric(12,5) NOT NULL,
    dim_x_cm numeric(12,5) NOT NULL,
    dim_y_cm numeric(12,5) NOT NULL,
    dim_z_cm numeric(12,5) NOT NULL,
    stock_image_path character varying(75) COLLATE pg_catalog."default",
    CONSTRAINT inventory_product_pkey PRIMARY KEY (id),
    CONSTRAINT inventory_product_asin_key UNIQUE (asin)
,
    CONSTRAINT inventory_product_ebay_sku_key UNIQUE (ebay_sku)
,
    CONSTRAINT inventory_product_stock_image_path_key UNIQUE (stock_image_path)
,
    CONSTRAINT inventory_product_tcgplayer_sku_key UNIQUE (tcgplayer_sku)

)

插入:

CREATE TABLE public.account_sale
(
    id integer NOT NULL DEFAULT nextval('account_sale_id_seq'::regclass),
    unit_price numeric(10,2) NOT NULL,
    qty integer NOT NULL,
    order_id integer NOT NULL,
    product_id integer,
    CONSTRAINT account_sale_pkey PRIMARY KEY (id),
CONSTRAINT account_sale_order_id_product_id_8c7f2e6a_uniq UNIQUE (order_id, product_id)
,
CONSTRAINT account_sale_order_id_7724b965_fk_account_order_id FOREIGN KEY (order_id)
        REFERENCES public.account_order (id) MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
        DEFERRABLE INITIALLY DEFERRED,
CONSTRAINT account_sale_product_id_716f2cb2_fk_inventory_product_id FOREIGN KEY (product_id)
        REFERENCES public.inventory_product (id) MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION
        DEFERRABLE INITIALLY DEFERRED
)

插入参数:

INSERT INTO account_sale (qty, unit_price, order_id, product_id)
SELECT $1::integer,$2::float,$3::integer,t.id FROM inventory_product 
AS t WHERE t.ebay_sku=$4 
UNION 
SELECT $5::integer,$6::float,$7::integer,t.id FROM inventory_product 
AS t WHERE t.ebay_sku=$8 

请注意,当我移除触发器时,插入效果很好。

我也正在从Node.js服务器运行插入(但是我认为这不重要)。

我在这里想念什么?

1 个答案:

答案 0 :(得分:3)

不要在SET分配的左侧使用目标的表别名。始终很清楚哪个表在那里。顺便说一句:函数语言是一种标识符,不应该用引号引起来:

CREATE OR REPLACE FUNCTION update_inventory()
RETURNS TRIGGER AS $$
BEGIN
  IF NEW.product_id IS NOT NULL THEN
    UPDATE inventory_product AS t
       SET qty = t.qty - NEW.qty
       -- ^ here
    WHERE t.id = NEW.product_id;
  END IF;
END; $$ LANGUAGE plpgsql;

实际上,您在UPDATE语句中根本不需要任何别名:

CREATE OR REPLACE FUNCTION update_inventory()
RETURNS TRIGGER AS $$
BEGIN
  IF NEW.product_id IS NOT NULL THEN
    UPDATE inventory_product 
       SET qty = qty - NEW.qty
    WHERE id = NEW.product_id;
  END IF;
END; $$ LANGUAGE plpgsql;