mysqlmysql-error-1005

MySQL error #1005 (Code 150)


I have tried create this table, but nothing I have tried works from FKs.

CREATE TABLE `tb_AutSituacao` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `Nome` varchar(50) CHARACTER SET latin1 NOT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8

CREATE TABLE `tb_AutHistorico` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `Situacao` int(11) NOT NULL,
 `Data` date NOT NULL,
 `Agente` int(11) NOT NULL,
 `Proposta` int(11) NOT NULL,
 PRIMARY KEY (`id`),
 KEY `AutHistorico_Situacao` (`Situacao`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8

ALTER TABLE `tb_AutHistorico` ADD FOREIGN KEY ( `Situacao` ) REFERENCES `sicor`.`tb_AutSituacao` (`id`) ON DELETE RESTRICT ON UPDATE RESTRICT;

I always get "#1005 - Can't create table 'tablename' (errno: 150)". Thanks for help.


Solution

  • $ perror 150
    MySQL error code 150: Foreign key constraint is incorrectly formed
    

    Fix your FOREIGN KEY definition.