Question

i have this sql clauses:

CREATE TABLE IF NOT EXISTS `culture` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `code` char(6) NOT NULL DEFAULT 'it',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;


CREATE TABLE IF NOT EXISTS `nations` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `culture_id` int(11) NOT NULL,
  `iso_code_2` char(2) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `nations_FI_1` (`culture_id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=245 ;


CREATE TABLE IF NOT EXISTS `sedi` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `nome` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=13 ;


CREATE TABLE IF NOT EXISTS `sedi_i18n` (
  `id` int(11) NOT NULL,
  `culture` char(6) NOT NULL,
  `nation` char(2) NOT NULL,
  `indirizzo` text NOT NULL,
  PRIMARY KEY (`id`,`culture`),
  KEY `sedi_i18n_FI_2` (`culture`),
  KEY `sedi_i18n_FI_3` (`nation`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


ALTER TABLE `sedi_i18n`
  ADD CONSTRAINT `sedi_i18n_FK_1` FOREIGN KEY (`id`) REFERENCES `sedi` (`id`) ON DELETE CASCADE,
  ADD CONSTRAINT `sedi_i18n_FK_2` FOREIGN KEY (`culture`) REFERENCES `culture` (`code`),
  ADD CONSTRAINT `sedi_i18n_FK_3` FOREIGN KEY (`nation`) REFERENCES `nations` (`iso_code_2`);

But i'm gettin this error:

Errore query SQL:

ALTER TABLE sedi_i18n ADD CONSTRAINT sedi_i18n_FK_1 FOREIGN KEY ( id ) REFERENCES sedi ( id ) ON DELETE CASCADE , ADD CONSTRAINT sedi_i18n_FK_2 FOREIGN KEY ( culture ) REFERENCES culture ( code ) , ADD CONSTRAINT sedi_i18n_FK_3 FOREIGN KEY ( nation ) REFERENCES nations ( iso_code_2 ) ;

Messaggio di MySQL:

1005 - Can't create table 'test_javier_4.#sql-528_aed' (errno: 150)

Any idea?

Regards

Javi

Was it helpful?

Solution

The problem is that 2 of your 3 foreign keys refer to columns in the parent table that are neither primary keys nor do they have a unique index on them.

All foreign key columns must reference either the primary key or a unique key in the parent table.

These are the two bad foreign keys:

  ADD CONSTRAINT `sedi_i18n_FK_2` FOREIGN KEY (`culture`) REFERENCES `culture` (`code`),
  ADD CONSTRAINT `sedi_i18n_FK_3` FOREIGN KEY (`nation`) REFERENCES `nations` (`iso_code_2`);

If those columns are meant to be unique in the parent table, then you may resolve this by adding unique indexes on them like this:

alter table `culture`
  add unique key (`code`);

alter table `nations`
  add unique key (`iso_code_2`);

OTHER TIPS

From the MySQL documentation:

1005 (ER_CANT_CREATE_TABLE)

Cannot create table. If the error message refers to error 150, table creation failed because a foreign key constraint was not correctly formed.

Can you isolate which of the three foreign key constraint additions is causing the error, by running them one at a time? Like:

ALTER TABLE `sedi_i18n`
  ADD CONSTRAINT `sedi_i18n_FK_1` FOREIGN KEY (`id`) REFERENCES `sedi` (`id`) ON DELETE CASCADE
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top