在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)关联外键的源列,而不是目标列.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。