I created the table in MySQL Workbench as follows:
ORDRE table:
CREATE TABLE Ordre ( OrdreID INT NOT NULL, OrdreDato DATE DEFAULT NULL, KundeID INT DEFAULT NULL, CONSTRAINT Ordre_pk PRIMARY KEY (OrdreID), CONSTRAINT Ordre_fk FOREIGN KEY (KundeID) REFERENCES Kunde (KundeID) ) ENGINE = InnoDB;
Product list:
CREATE TABLE Produkt ( ProduktID INT NOT NULL, ProduktBeskrivelse VARCHAR(100) DEFAULT NULL, ProduktFarge VARCHAR(20) DEFAULT NULL, Enhetpris INT DEFAULT NULL, CONSTRAINT Produkt_pk PRIMARY KEY (ProduktID) ) ENGINE = InnoDB;
and ORDRELINJE tables:
CREATE TABLE Ordrelinje ( Ordre INT NOT NULL, Produkt INT NOT NULL, AntallBestilt INT DEFAULT NULL, CONSTRAINT Ordrelinje_pk PRIMARY KEY (Ordre, Produkt), CONSTRAINT Ordrelinje_fk FOREIGN KEY (Ordre) REFERENCES Ordre (OrdreID), CONSTRAINT Ordrelinje_fk1 FOREIGN KEY (Produkt) REFERENCES Produkt (ProduktID) ) ENGINE = InnoDB;
So when I try to insert a value into the ORDRELINJE
table I get:
Error code: 1452. Unable to add or update child row: Foreign key constraint failed (
srdjank
.Ordrelinje
, constraintOrdrelinje_fk
foreign key (Ordre
) ReferenceOrd re
(OrdreID
) )
I've seen other posts on this topic but no luck. Am I supervising something or knowing what to do?
Taken fromUsing foreign key constraints一个>
Thus, your error
Error code: 1452. Unable to add or update child row: Foreign key constraint failure
Essentially means, you are trying to add a row toOrdrelinje
Ordre
The code> for the matching row (OrderID) does not exist in the table surface.You must first insert the row into the
Ordre
table.