Estoy tratando de reenviar mi nuevo esquema en mi servidor db, pero no puedo averiguar por qué estoy recibiendo este error. He intentado buscar la respuesta aquí, pero todo lo que he encontrado me ha dicho que configure el motor de la base de datos como Innodb o que me asegure de que las claves que estoy intentando utilizar como claves externas son claves primarias en sus propias tablas. He hecho ambas cosas, si no me equivoco. ¿Alguna otra ayuda que podáis ofrecer?
Executing SQL script in server
ERROR: Error 1215: Cannot add foreign key constraint
-- -----------------------------------------------------
-- Table `Alternative_Pathways`.`Clients_has_Staff`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `Alternative_Pathways`.`Clients_has_Staff` (
`Clients_Case_Number` INT NOT NULL ,
`Staff_Emp_ID` INT NOT NULL ,
PRIMARY KEY (`Clients_Case_Number`, `Staff_Emp_ID`) ,
INDEX `fk_Clients_has_Staff_Staff1_idx` (`Staff_Emp_ID` ASC) ,
INDEX `fk_Clients_has_Staff_Clients_idx` (`Clients_Case_Number` ASC) ,
CONSTRAINT `fk_Clients_has_Staff_Clients`
FOREIGN KEY (`Clients_Case_Number` )
REFERENCES `Alternative_Pathways`.`Clients` (`Case_Number` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_Clients_has_Staff_Staff1`
FOREIGN KEY (`Staff_Emp_ID` )
REFERENCES `Alternative_Pathways`.`Staff` (`Emp_ID` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
Ejecución del script SQL finalizada: sentencias: 7 exitosas, 1 fallida
Aquí está el SQL para las tablas de los padres.
CREATE TABLE IF NOT EXISTS `Alternative_Pathways`.`Clients` (
`Case_Number` INT NOT NULL ,
`First_Name` CHAR(10) NULL ,
`Middle_Name` CHAR(10) NULL ,
`Last_Name` CHAR(10) NULL ,
`Address` CHAR(50) NULL ,
`Phone_Number` INT(10) NULL ,
PRIMARY KEY (`Case_Number`) )
ENGINE = InnoDB
CREATE TABLE IF NOT EXISTS `Alternative_Pathways`.`Staff` (
`Emp_ID` INT NOT NULL ,
`First_Name` CHAR(10) NULL ,
`Middle_Name` CHAR(10) NULL ,
`Last_Name` CHAR(10) NULL ,
PRIMARY KEY (`Emp_ID`) )
ENGINE = InnoDB
Supongo que Clients.Case_Number
y/o Staff.Emp_ID
no son exactamente el mismo tipo de datos que Clients_has_Staff.Clients_Case_Number
y Clients_has_Staff.Staff_Emp_ID
.
¿Quizás las columnas de las tablas padre son INT UNSIGNED
?
Tienen que ser exactamente el mismo tipo de datos en ambas tablas.
Razones por las que puede obtener un error de restricción de clave foránea:
Actualización:
ON DELETE SET NULL
no esté definida como nula. Así que asegúrese de que la columna se establece por defecto null.Compruebe esto.