简体   繁体   中英

#1215 - Cannot add foreign key constraint

I'm working a project with Yiiframwork and I have this table in my data base project

CREATE TABLE IF NOT EXISTS `tbl_annonce` (
  `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
  `idEntreprise` tinyint(3) unsigned NOT NULL 
   COMMENT 'CONSTRAINT FOREIGN KEY (idEntreprise) REFERENCES tbl_entreprise(id)',
  `titre` varchar(100) NOT NULL,
  `detailleDiscription` varchar(5000) NOT NULL,
  `categorie` varchar(100) DEFAULT NULL,
  `typePoste` varchar(100) NOT NULL,
  `salaireMin` varchar(80) NOT NULL,
  `salaireMax` varchar(80) NOT NULL,
  `niveauEtude` varchar(80) NOT NULL,
  `niveauExperience` varchar(80) NOT NULL,
  `langue` varchar(50) DEFAULT NULL,
  `poste` varchar(50) NOT NULL,
  `pays` varchar(50) NOT NULL,
  `ville` varchar(50) NOT NULL,
  `adresse` varchar(80) NOT NULL,
  `datePublication` timestamp NOT NULL 
       DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  `etat` varchar(50) NOT NULL,
  `photo` varchar(255)  NULL,
  `nometr` text NOT NULL,
  PRIMARY KEY (`id`),
  CONSTRAINT `fk_idEntrepriseAnn` 
      FOREIGN KEY (idEntreprise) 
      REFERENCES tbl_entreprise(id)
      ON DELETE CASCADE ON UPDATE CASCADE
    ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

I only use 'comment' because I found it in a tutorial for a yii framework project. I have got the attribute ' idEntreprise ' as FK in my DB but when I entred other 'idEntreprise' that does note exist in my table entreprise it was not problem, instead it must be a problem.

So I added 'identreprise' as Fk and that have this problem now.

Hope you understand my problem :/

can any one helep me plz !!

To define a foreign key on a column, it must fulfil some conditions.

  1. Child and parent column must have same column definition by type, signed.
  2. Parent column must have an index defined on it.

Make sure that

  1. The column tbl_entreprise.id is indexed.
  2. The column definition idEntreprise tinyint(3) unsigned in tbl_annonce matches with that of tbl_entreprise.id

Refer to : MySQL: Foregin Key Constaints

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM