![](/img/trans.png)
[英]Insert or Update on Table Violates Foreign Key Constraint with Postgres
[英]insert or update on table violates foreign key constraint
我有兩個表: entitytype和project 。 以下是創建表語句:
Create table project (
pname varchar(20) not null,
primary key(pname)
);
create table entitytype(
entityname varchar(20) not null,
toppos char(100),
leftpos char(100),
pname varchar(20) not null,
primary key(entityname),
foreign key(pname) references project(pname) on delete cascade on update cascade
);
當我嘗試將任何值插入到entitytype表中時,出現以下錯誤:
ERROR: insert or update on table "entitytype" violates foreign key constraint "entitytype_pname_fkey"
Detail: Key (pname)=(494) is not present in table "project".
任何人都可以闡明我做錯了什么嗎?
該錯誤消息意味着您正在嘗試添加沒有相應項目條目的 entityType。 (我不知道您的領域或您想要實現的目標,但該架構設計在我看來是錯誤的...)
您在表項目中沒有 pname 為(在您的示例中)494 的記錄嗎?
鍵關系表示實體表中不允許有 pname,除非它與項目表中的 pname 匹配。
當您的架構錯誤時會發生此錯誤。 提示:再次檢查您的外鍵分配
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.