简体   繁体   English

#1215-无法添加外键约束

[英]#1215 - Cannot add foreign key constraint

I'm working a project with Yiiframwork and I have this table in my data base project 我正在与Yiiframwork合作一个项目,并且我的数据库项目中有此表

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. 我只使用'comment'因为我在yii框架项目的教程中找到了它。 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. 我在数据库中将属性' idEntreprise '设置为FK,但是当我输入确实在表entreprise存在的其他'idEntreprise' ,这不是问题,而是一定有问题。

So I added 'identreprise' as Fk and that have this problem now. 因此,我将'identreprise'添加为Fk,现在出现了此问题。

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. 索引tbl_entreprise.id列。
  2. The column definition idEntreprise tinyint(3) unsigned in tbl_annonce matches with that of tbl_entreprise.id 列定义idEntreprise tinyint(3) unsignedtbl_annonce与相匹配tbl_entreprise.id

Refer to : MySQL: Foregin Key Constaints 参考MySQL:Foregin关键内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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