Inserting or updating a table violates a foreign key constraint

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

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 light on what I'm doing wrong?

+4
source share
2 answers

The error message means that you are trying to add an entityType that does not have a matching Project record. (I don’t know your domain or what you are trying to achieve, but the design of the circuit looks wrong to me ...)

+8
source

You do not have an entry in the table project with pname (in your example) 494?

The key relation says that no pname is not allowed in the entity table if it does not match pname in the project table.

+3
source

Source: https://habr.com/ru/post/1304112/


All Articles