suprasad
suprasad

Reputation: 1533

insert or update on table violates foreign key constraint

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

When I try to insert any values into the entitytype table, I get 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 anyone shed some light on what I am doing wrong?

Upvotes: 39

Views: 192474

Answers (3)

Aswath M
Aswath M

Reputation: 21

I have obtained the same issue (InnerException = {"23503: insert or update on table "table-name" violates foreign key constraint "tabename""})

This issue may arise if the table have updated version than the entry side version . because there might be a possibility of adding of additional columns in that updated version table.

  • Check your entry properly.
  • Check the table(package version) for all available columns.

Upvotes: 1

Mitch Wheat
Mitch Wheat

Reputation: 300797

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

Upvotes: 32

Larry Lustig
Larry Lustig

Reputation: 51008

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.

Upvotes: 11

Related Questions