Mysql
 sql >> Base de données >  >> RDS >> Mysql

ERREUR 1215. MySql InnoDB

J'ai testé la création de votre tableau.

Ensuite, j'ai obtenu plus d'informations sur l'erreur de clé étrangère :

mysql> show engine innodb status\G

------------------------
LATEST FOREIGN KEY ERROR
------------------------
2018-02-20 14:51:33 700002d90000 Error in foreign key constraint of table calls/called:

    FOREIGN KEY (`Code`)
    REFERENCES `calls`.`city` (`Code`)
    ON DELETE CASCADE
    ON UPDATE CASCADE,
  CONSTRAINT `Number`
    FOREIGN KEY (`Number`)
    REFERENCES `calls`.`subscriber` (`Number`)
    ON DELETE CASCADE
    ON UPDATE CASCADE):
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
...

Je vois le problème :vous avez une clé primaire composée dans city sur les colonnes (Name, Code) . Pour cela, vous devez en créer un contrainte de clé étrangère référençant les deux colonnes de la clé primaire du parent.

Comme ceci :

CONSTRAINT `Name`
FOREIGN KEY (`Name`, `Code`)
REFERENCES `calls`.`city` (`Name`, `Code`)
ON DELETE CASCADE
ON UPDATE CASCADE

Ne déclarez pas de contrainte pour chaque colonne — déclarez une contrainte qui fait référence aux deux colonnes de la clé.