2010-03-15 73 views
4

我有两个表:实体类型项目。下面是创建表的语句:在表上插入或更新违反外键约束

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". 

任何人都可以摆脱对我是什么的一些光做错了?

回答

7

错误消息表示您正在尝试添加没有相应项目条目的entityType。 (我不知道你的域名或你想要实现的目标,但是这种模式设计对我来说是错误的...)

+1

嘿,非常感谢,我想我该怎么做错了。当我输入的数据是以错误的顺序插入的。 – suprasad 2010-03-15 03:26:33

3

您是否在表项目中没有记录(在您的示例中为494)?

关键关系表示在实体表中不允许使用pname,除非它与项目表中的pname匹配。

+0

非常感谢您的意见。我以错误的顺序插入数据。 – suprasad 2010-03-15 03:27:01

相关问题