Search code examples
mysqlphpmyadminforeign-keysmysql-error-150

error 150 creating table with mysql with foreign keys


I'm not really sure why i'm getting error 150 from phpmyadmin in my web-server, when i create the tables in local it works fine, maybe I'm missing something.

Here's my code:

Table usuarios

CREATE  TABLE IF NOT EXISTS `usuarios` (
`id` INT NOT NULL AUTO_INCREMENT COMMENT 'LLave primaria de la tabla' ,
`nombre` VARCHAR(100) NULL COMMENT 'Nombre completo del usuario' ,
`email` VARCHAR(75) NULL COMMENT 'Login del usuario' ,
`password` VARCHAR(45) NULL COMMENT 'Clave del usuario' ,
`fecha_registro` DATETIME NULL COMMENT 'Fecha en la que se registro el usuario' ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB;

Table perfiles:

CREATE  TABLE IF NOT EXISTS `perfiles` (
`id` INT NOT NULL AUTO_INCREMENT COMMENT 'llave primaria de la tabla' ,
`nombre` VARCHAR(45) NULL COMMENT 'Descripción del perfil' ,
`fecha_registro` DATETIME NULL COMMENT 'fecha de registro' ,
PRIMARY KEY (`id`) )
ENGINE = InnoDB;

Table usuarios_perfiles:

CREATE  TABLE IF NOT EXISTS `usuarios_perfiles` (
`usuario_id` INT NOT NULL ,
`perfil_id` INT NOT NULL ,
INDEX `fk_usuarios_perfiles_usuarios_idx` (`usuario_id` ASC) ,
INDEX `fk_usuarios_perfiles_perfiles1_idx` (`perfil_id` ASC) ,
CONSTRAINT `fk_usuarios_perfiles_usuarios`
FOREIGN KEY (`usuario_id` )
REFERENCES `usuarios` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_usuarios_perfiles_perfiles1`
FOREIGN KEY (`perfil_id` )
REFERENCES `perfiles` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;

When i'm running all this code in my local server it works fine, but when i want to create this in phpmyadmin from my web-server it send error 105.


Solution

  • The solution was adding the ENGINE INNODB, When i create the usuarios table i didn't put the last line ENGINE = InnoDB; and i was getting MyISAM.

    It seems like MyISAM don't let me use fk. When i change the ENGINE to InnoDB everything start to run properly.

    Thanks all!