Foreign key constraint failed: cannot add or update child row (Error 1452)
P粉155551728
P粉155551728 2023-10-08 23:40:09
0
1
803

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, constraint Ordrelinje_fk foreign key (Ordre) Reference Ord re (OrdreID) )

I've seen other posts on this topic but no luck. Am I supervising something or knowing what to do?

P粉155551728
P粉155551728

reply all(1)
P粉410239819

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 to Ordrelinje 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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!