加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

postgresql – 表格约束中不存在postgres键

发布时间:2020-12-13 18:10:33 所属栏目:百科 来源:网络整理
导读:在Postgres 9.5中尝试ALTER TABLE创建外键约束时:从product_template.product_brand_id到product_brand.id ALTER TABLE public.product_template ADD CONSTRAINT product_template_product_brand_id_fkey FOREIGN KEY (product_brand_id) REFERENCES public
在Postgres 9.5中尝试ALTER TABLE创建外键约束时:从product_template.product_brand_id到product_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;

返回错误

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

我很困惑为什么postgres试图找到product_brand.product_brand_id,当fkey是从product_template.product_brand_id到product_brand.id

有任何想法吗?

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

但是table_brand表中没有对应的行,其中列id包含值12

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读