views:

227

answers:

2

Hi,

I have two tables entitytype and project. Here are the create table statements:

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
);

Now when i try to insert any values into entitytype table i am getting the following error:

ERROR: insert or update on table "entitytype" violates foreign key constraint "entitytype_pname_fkey"
  Detail: Key (pname)=(494) is not present in table "project".

Can someone please shed some light on what i am doing wrong. Any input will be of great help.

Thank you.

+1  A: 

Do you not have a record in table project with a pname of (in your example) 494?

The key relationship says no pname is allowed in the entity table unless it matches a pname in the project table.

Larry Lustig
Thank you so much for your input. i was inserting data in wrong order.
sprasad12
+1  A: 

The error message means you are attempting to add an entityType that does not have a corresponding Project entry. (I don't know your domain or what you are trying to achieve, but that schema design looks wrong to me...)

Mitch Wheat
hey thanks a lot i figured what i was going wrong. when putting in the data i was inserting in wrong order.
sprasad12