postgres键不存在于表约束中

Ada*_*ims 7 postgresql foreign-keys postgresql-9.5

在Postgres 9.5中尝试ALTER TABLE创建外键约束时:from product_template.product_brand_idtoproduct_brand.id

ALTER TABLE public.product_template
    ADD CONSTRAINT product_template_product_brand_id_fkey 
    FOREIGN KEY (product_brand_id)
    REFERENCES public.product_brand (id) MATCH SIMPLE
    ON UPDATE NO ACTION
    ON DELETE SET NULL;
Run Code Online (Sandbox Code Playgroud)

返回错误

ERROR:  insert or update on table "product_template" violates foreign key         constraint "product_template_product_brand_id_fkey"
DETAIL:  Key (product_brand_id)=(12) is not present in table "product_brand".
STATEMENT:  ALTER TABLE "product_template" ADD FOREIGN KEY ("product_brand_id") REFERENCES "product_brand" ON DELETE set null
Run Code Online (Sandbox Code Playgroud)

林困惑,为什么Postgres的是试图找到product_brand.product_brand_id,当FKEY是product_template.product_brand_idproduct_brand.id

有任何想法吗?

a_h*_*ame 8

错误消息只是指出表product_template中至少有一行包含12列中的值product_brand_id

但是表product_brand中没有对应的行id包含该列的值12

Key (product_brand_id)=(12)关联外键的列,而不是目标列。