1
I am trying to reference the column id of a table in another table in my database, but I am told an error that is not very specific. The engine being used is Innodb and the database is in Mysql. It follows script generated by the database and error reported respectively
Sequel:
ALTER TABLE `service_desk_pi`.`chamado`
ADD CONSTRAINT `estrangeiraEquipamento`
FOREIGN KEY (`fkIdEquipamento`)
REFERENCES `service_desk_pi`.`equipamento` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE;
Error:
Executing:
ALTER TABLE `service_desk_pi`.`chamado`
ADD CONSTRAINT `estrangeiraEquipamento`
FOREIGN KEY (`fkIdEquipamento`)
REFERENCES `service_desk_pi`.`equipamento` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE;
Operation failed: There was an error while applying the SQL script to the database.
ERROR 1215: Cannot add foreign key constraint
SQL Statement:
ALTER TABLE `service_desk_pi`.`chamado`
ADD CONSTRAINT `estrangeiraEquipamento`
FOREIGN KEY (`fkIdEquipamento`)
REFERENCES `service_desk_pi`.`equipamento` (`id`)
ON DELETE CASCADE
ON UPDATE CASCADE
Here
REFERENCES service_desk_pishould probably be the name of your equipment table.– Bruno Warmling
This script with this syntax is generated by the database itself, right after REFERENCES
service_desk_pione has .equipamento(id) which references the table name and also the column name. Can the database itself make a mistake in the syntax of the script it produces?– João Gutierre
Has the table that you are trying to reference with the foreign key been successfully inserted? Could you put in its script as well?
– mbissonho
The table is already persisted in the database. Here is the script: CREATE TABLE
equipamento(idint(11) NOT NULL AUTO_INCREMENT,fk_chamadovarchar(45) NOT NULL,marcavarchar(45) NOT NULL,modelovarchar(45) NOT NULL,categoriavarchar(45) NOT NULL, PRIMARY KEY (id) ) ENGINE=Myisam DEFAULT CHARSET=latin1; ?– João Gutierre