FEHLER 1452: Eine untergeordnete Zeile kann nicht hinzugefügt oder aktualisiert werden: Eine Fremdschlüsseleinschränkung schlägt fehl
Ich habe mit MySQL Workbench 5.2.47 zwei Tabellen in MySQL 5.6.11 erstellt, wie unten gezeigt.
Dascountry
Tabelle:
delimiter $
CREATE TABLE `country` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`country_name` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INC
REMENT=2 DEFAULT CHARSET=utf8$
Dasstate_table
:
delimiter $
CREATE TABLE `state_table` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`state_name` varchar(45) DEFAULT NULL,
`country_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
CONSTRAINT `country_fk` FOREIGN KEY (`id`) REFERENCES `country` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COMMENT=''$
Es gibt eine Reihe in dercountry
Tisch mit demid
1. Es kann nur eine (untergeordnete) Zeile in die untergeordnete Tabelle eingefügt werdenstate_table
. Wenn mehr Zeilen versucht werden, tritt der folgende Fehler auf.
FEHLER 1452: Eine untergeordnete Zeile kann nicht hinzugefügt oder aktualisiert werden: Eine Fremdschlüsseleinschränkung schlägt fehl (social_networking
.state_table
, EINSCHRÄNKUNGcountry_fk
UNBEKANNTER SCHLÜSSEL (id
) VERWEISEcountry
(id
) ON DELETE CASCADE ON UPDATE CASCADE)
SQL-Anweisung:
INSERT INTO `social_networking`.`state_table` (`id`, `state_name`, `country_id`) VALUES ('2', 'xxx', '1')
Eigentlich versuche ich, diese Tabellen mit einem ORM (JPA) abzubilden, wo ich immer nur seheOneToOne
Beziehung.
Was vermisse ich?