Jag lyckas inte uppdatera mina tabeller efter som jag får ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails när jag försöker göra en insert.
Importen från CSV-filer fungerar utmärkt och det är inga problem där, men att göra en insert i en Child tabel fungerar bara inte.
Det här verkar gälla alla tabeller i databasen. Det enklaste exemplet som jag felsöker på just nu är med följande två tabeller:
CREATE TABLE IF NOT EXISTS `eshop`.`forlag` (
`namn` VARCHAR(45) NOT NULL,
PRIMARY KEY (`namn`))
CHARACTER SET utf8mb4
COLLATE utf8mb4_swedish_ci
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `eshop`.`bok`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `eshop`.`bok` (
`isbn` CHAR(13) NOT NULL,
`titel` VARCHAR(45) NOT NULL,
`pris` DECIMAL(5, 2) NULL,
-- `antal` INT NULL,
`forlag_namn` VARCHAR(45) NOT NULL,
PRIMARY KEY (`isbn`),
INDEX `fk_bok_har_forlag` (`forlag_namn` ASC) VISIBLE,
UNIQUE INDEX `isbn_UNIQUE` (`isbn` ASC) VISIBLE,
CONSTRAINT `fk_bok_har_forlag`
FOREIGN KEY (`forlag_namn`)
REFERENCES `eshop`.`forlag` (`namn`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
CHARACTER SET utf8mb4
COLLATE utf8mb4_swedish_ci
ENGINE = InnoDB;
Jag gör sedan en
insert into forlag values ('saga_press');
som funkar utmärkt och sedan en
insert into bok values (isbn = 13131313132, titel = 'idsf', pris = 1, forlag_namn = 'saga_press');
och får då
MySQL [eshop]> insert into bok values (isdn = 13131313132, titel = 'idsf', pris = 1, antal = 2, `forlag_namn` = 'saga_press');
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`eshop`.`bok`, CONSTRAINT `fk_bok_har_forlag` FOREIGN KEY (`forlag_namn`) REFERENCES `forlag` (`namn`))
MySQL [eshop]> insert into bok values (isbn = 13131313132, titel = 'idsf', pris = 1, forlag_namn = 'saga_press');
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`eshop`.`bok`, CONSTRAINT `fk_bok_har_forlag` FOREIGN KEY (`forlag_namn`) REFERENCES `forlag` (`namn`))
Samma fel uppstår i både Workbench och terminalen. Jag kör Debian på en windowsdator.
SHOW Engine innodb status ger följande:
------------------------
LATEST FOREIGN KEY ERROR
------------------------
2020-03-15 13:12:45 0xc4c Transaction:
TRANSACTION 27432, ACTIVE 0 sec inserting, thread declared inside InnoDB 5000
mysql tables in use 1, locked 1
3 lock struct(s), heap size 1136, 1 row lock(s), undo log entries 1
MySQL thread id 34, OS thread handle 3148, query id 1498 localhost 127.0.0.1 dbwebb update
insert into bok values (isbn = 13131313132, titel = 'idsf', pris = 1, forlag_namn = 'saga_press')
Foreign key constraint fails for table `eshop`.`bok`:
,
CONSTRAINT `fk_bok_har_forlag` FOREIGN KEY (`forlag_namn`) REFERENCES `forlag` (`namn`)
Trying to add in child table, in index fk_bok_har_forlag tuple:
DATA TUPLE: 2 fields;
0: len 1; hex 30; asc 0;;
1: len 13; hex 30202020202020202020202020; asc 0 ;;
But in parent table `eshop`.`forlag`, in index PRIMARY,
the closest match we can find is record:
PHYSICAL RECORD: n_fields 3; compact format; info bits 0
0: len 10; hex 736167615f7072657373; asc saga_press;;
1: len 6; hex 000000006b25; asc k%;;
2: len 7; hex 82000000ba0110; asc ;;
Importen från CSV-filer fungerar utmärkt och det är inga problem där, men att göra en insert i en Child tabel fungerar bara inte.
Det här verkar gälla alla tabeller i databasen. Det enklaste exemplet som jag felsöker på just nu är med följande två tabeller:
CREATE TABLE IF NOT EXISTS `eshop`.`forlag` (
`namn` VARCHAR(45) NOT NULL,
PRIMARY KEY (`namn`))
CHARACTER SET utf8mb4
COLLATE utf8mb4_swedish_ci
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `eshop`.`bok`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `eshop`.`bok` (
`isbn` CHAR(13) NOT NULL,
`titel` VARCHAR(45) NOT NULL,
`pris` DECIMAL(5, 2) NULL,
-- `antal` INT NULL,
`forlag_namn` VARCHAR(45) NOT NULL,
PRIMARY KEY (`isbn`),
INDEX `fk_bok_har_forlag` (`forlag_namn` ASC) VISIBLE,
UNIQUE INDEX `isbn_UNIQUE` (`isbn` ASC) VISIBLE,
CONSTRAINT `fk_bok_har_forlag`
FOREIGN KEY (`forlag_namn`)
REFERENCES `eshop`.`forlag` (`namn`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
CHARACTER SET utf8mb4
COLLATE utf8mb4_swedish_ci
ENGINE = InnoDB;
Jag gör sedan en
insert into forlag values ('saga_press');
som funkar utmärkt och sedan en
insert into bok values (isbn = 13131313132, titel = 'idsf', pris = 1, forlag_namn = 'saga_press');
och får då
MySQL [eshop]> insert into bok values (isdn = 13131313132, titel = 'idsf', pris = 1, antal = 2, `forlag_namn` = 'saga_press');
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`eshop`.`bok`, CONSTRAINT `fk_bok_har_forlag` FOREIGN KEY (`forlag_namn`) REFERENCES `forlag` (`namn`))
MySQL [eshop]> insert into bok values (isbn = 13131313132, titel = 'idsf', pris = 1, forlag_namn = 'saga_press');
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`eshop`.`bok`, CONSTRAINT `fk_bok_har_forlag` FOREIGN KEY (`forlag_namn`) REFERENCES `forlag` (`namn`))
Samma fel uppstår i både Workbench och terminalen. Jag kör Debian på en windowsdator.
SHOW Engine innodb status ger följande:
------------------------
LATEST FOREIGN KEY ERROR
------------------------
2020-03-15 13:12:45 0xc4c Transaction:
TRANSACTION 27432, ACTIVE 0 sec inserting, thread declared inside InnoDB 5000
mysql tables in use 1, locked 1
3 lock struct(s), heap size 1136, 1 row lock(s), undo log entries 1
MySQL thread id 34, OS thread handle 3148, query id 1498 localhost 127.0.0.1 dbwebb update
insert into bok values (isbn = 13131313132, titel = 'idsf', pris = 1, forlag_namn = 'saga_press')
Foreign key constraint fails for table `eshop`.`bok`:
,
CONSTRAINT `fk_bok_har_forlag` FOREIGN KEY (`forlag_namn`) REFERENCES `forlag` (`namn`)
Trying to add in child table, in index fk_bok_har_forlag tuple:
DATA TUPLE: 2 fields;
0: len 1; hex 30; asc 0;;
1: len 13; hex 30202020202020202020202020; asc 0 ;;
But in parent table `eshop`.`forlag`, in index PRIMARY,
the closest match we can find is record:
PHYSICAL RECORD: n_fields 3; compact format; info bits 0
0: len 10; hex 736167615f7072657373; asc saga_press;;
1: len 6; hex 000000006b25; asc k%;;
2: len 7; hex 82000000ba0110; asc ;;