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

久未见 提交于 2019-12-05 05:09:37

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;

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!