MySQL Error : #1005 - Can't create table (errno: 150) When I try create more than 1 FK
Asked Answered
L

2

7

I have this table:

CREATE TABLE IF NOT EXISTS `produtos` (
  `id` int(11) NOT NULL auto_increment,
  `idcatprodutos` int(11) NOT NULL,
  `idcategoria` int(11) NOT NULL,
  `idmarca` int(11) NOT NULL,
  `nome` varchar(100) NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `FK_produtos_2` (`idcatprodutos`),
  KEY `FK_produtos_3` (`idmarca`),
  KEY `FK_produtos_4` (`idcategoria`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 ROW_FORMAT=DYNAMIC AUTO_INCREMENT=39 ;

and this table:

CREATE TABLE IF NOT EXISTS `sugestoes` (
  `id` int(11) NOT NULL auto_increment,
  `idproduto` int(11) NOT NULL,
  `idsugestao1` int(11) NOT NULL,
  `idsugestao2` int(11) NOT NULL,
  `idsugestao3` int(11) NOT NULL,
  `idsugestao4` int(11) NOT NULL,
  PRIMARY KEY  (`id`),
  KEY `FK_sugestoes_prod` (`idproduto`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 ROW_FORMAT=FIXED AUTO_INCREMENT=9 ;

I already have created a fk sugestoes.idproduto -> produtos.id working, but I want each of the other fields also refer to the produtos.id through new FK. Run this command below that return MySQL Error : #1005 - Can't create table (errno: 150):

ALTER TABLE `infantile`.`sugestoes` ADD CONSTRAINT `FK_sugestoes_2` FOREIGN KEY `FK_sugestoes_2` (`idsugestao1`)
    REFERENCES `produtos` (`id`)
    ON DELETE SET NULL
    ON UPDATE CASCADE
, ROW_FORMAT = FIXED;

Does anyone have any idea what's going on?

Lakitalaks answered 30/3, 2012 at 5:35 Comment(1)
Why do you add that , ROW_FORMAT = FIXED in the ALTER TABLE ?Frey
B
10

Try this,

it works:

ALTER TABLE `sugestoes`
ADD CONSTRAINT `FK_idproduto_produtos_1` FOREIGN KEY (`idproduto`) REFERENCES `produtos` (`id`),
ADD CONSTRAINT `FK_sugestoes_produtos_2` FOREIGN KEY (`idsugestao1`) REFERENCES `produtos` (`id`),
ADD CONSTRAINT `FK_sugestoes_produtos_3` FOREIGN KEY (`idsugestao2`) REFERENCES `produtos` (`id`),
ADD CONSTRAINT `FK_sugestoes_produtos_4` FOREIGN KEY (`idsugestao3`) REFERENCES `produtos` (`id`),  
ADD CONSTRAINT `FK_sugestoes_produtos_5` FOREIGN KEY (`idsugestao4`) REFERENCES `produtos` (`id`)

UPDATE:

You can not specify

ON DELETE SET NULL

Because of this:

You have defined a SET NULL condition though some of the columns are defined as NOT NULL

You can see exact error when you run

SHOW ENGINE INNODB STATUS;
Bakeman answered 30/3, 2012 at 5:52 Comment(2)
Longer works does not solve my problem, I need ON DELETE SET NULL and SET OnUpdate Cascade, when I try to make these changes, gives the same error message.Lakitalaks
I need when the user DELETE some produto.idproduto the corresponding sugestao.idsugestao be null or empty, how can I set it?Lakitalaks
C
0

Perhaps removing the ROW_FORMAT = FIXED:

ALTER TABLE `infantile`.`sugestoes` 
  ADD CONSTRAINT `FK_sugestoes_2` 
    FOREIGN KEY `FK_sugestoes_2` (`idsugestao1`)
      REFERENCES `produtos` (`id`)
      ON DELETE SET NULL
      ON UPDATE CASCADE
;

On second thought, how do you expect the ON DELETE SET NULL to behave when your column is defined with NOT NULL ?


And third, does the table has any data in it? If some of the rows violate this FK constraint, then you can't create that FK.

Clandestine answered 30/3, 2012 at 6:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.