错误1215:无法添加外键约束 [英] Error 1215: Cannot add foreign key constraint

查看:133
本文介绍了错误1215:无法添加外键约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用mySql Workbench构建了一个数据库,但是当我尝试将模型转发给服务器时,出现以下错误:

I built a database with mySql Workbench, but when I try to forward engineer my model to the server, I get the following error :

错误:错误1215:无法添加外键约束

ERROR: Error 1215: Cannot add foreign key constraint

接着是定义外键的表的定义,salaire_annee_ca

followed by the definition of the table where the foreign key is defined, salaire_annee_ca

我阅读了类似的主题,以确定导致此错误的常见原因,并进行了检查:

I read similar topics to identify the usual causes for this error, and checked :

  • salaire_annee_ca中定义的外键是否引用了另一个表的主键
  • 如果代码中的某些内容允许我的密钥为空,那是不允许的
  • 如果引用的类型和外键的类型相同
  • if the foreign key defined in salaire_annee_ca references the primary key of another table, which it does
  • if something in the code allowed my key to be null, which it doesn't
  • if the types of the reference and of the foreign key were the same

在我看来,所有这些条件都可以,所以我不明白为什么我仍然收到该消息.这是我的表的定义:

It seems to me that all these conditions are ok, so I don't understand why I still get that message. Here are the definitions of my tables :

这是两个主要的:

-- Table `credit_impot_db`.`salaires_annee`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `credit_impot_db`.`salaires_annee` (
  `salaire_annee_id` INT(11) NOT NULL ,
  `salaire_annuel` DOUBLE NOT NULL DEFAULT 0 ,
  `heures_travaillees` DOUBLE NOT NULL DEFAULT 0 ,
  `pourcentage_rsde` DOUBLE NOT NULL DEFAULT 0 ,
  `jours_travailles` INT(3) NOT NULL DEFAULT 0 ,
  PRIMARY KEY (`salaire_annee_id`) ,
  CONSTRAINT `salaire_annee_id`
    FOREIGN KEY (`salaire_annee_id` )
    REFERENCES `credit_impot_db`.`employes_ac` (`employe_ac_id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB;

This one is at the origin of the message :
-- -----------------------------------------------------
-- Table `credit_impot_db`.`salaire_annee_ca`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `credit_impot_db`.`salaire_annee_ca` (
  `salaire_annee_ca_id` INT(11) NOT NULL ,
  PRIMARY KEY (`salaire_annee_ca_id`) ,
  CONSTRAINT `salaire_annee_ca_id`
    FOREIGN KEY (`salaire_annee_ca_id` )
    REFERENCES `credit_impot_db`.`salaires_annee` (`salaire_annee_id` )
    ON DELETE CASCADE
    ON UPDATE CASCADE)
ENGINE = InnoDB;

并且还引用了以下两个:

And the following two are also referenced :

-- -----------------------------------------------------
-- Table `credit_impot_db`.`employes`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `credit_impot_db`.`employes` (
  `employe_id` INT(11) NOT NULL AUTO_INCREMENT ,
  `employe_nom` VARCHAR(255) NOT NULL ,
  `employe_prenom` VARCHAR(255) NOT NULL ,
  `employe_fonction` VARCHAR(255) NULL ,
  `employe_experience` VARCHAR(255) NULL DEFAULT NULL ,
  PRIMARY KEY (`employe_id`) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `credit_impot_db`.`employes_ac`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `credit_impot_db`.`employes_ac` (
  `employe_ac_id` INT(11) NOT NULL AUTO_INCREMENT ,
  `fk_employe_ac_employe_id` INT(11) NULL ,
  `fk_employe_ac_ac_id` INT(11) NULL ,
  PRIMARY KEY (`employe_ac_id`) ,
  INDEX `fk_employe_ac_employe_id_idx` (`fk_employe_ac_employe_id` ASC) ,
  INDEX `fk_employe_ac_ac_id_idx` (`fk_employe_ac_ac_id` ASC) ,
  CONSTRAINT `fk_employe_ac_employe_id`
    FOREIGN KEY (`fk_employe_ac_employe_id` )
    REFERENCES `credit_impot_db`.`employes` (`employe_id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_employe_ac_ac_id`
    FOREIGN KEY (`fk_employe_ac_ac_id` )
    REFERENCES `credit_impot_db`.`dossier_client` (`ac_id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

任何帮助将不胜感激!

推荐答案

好的,我想我已经知道了.这似乎是mySql Workbench的问题,如果:

Ok I think I figured it out. It seems to be a problem with mySql Workbench, the error disappears if :

  • 我首先在salaire_annee_ca中创建主键,
  • 然后正向工程我的数据库
  • 将我的主键声明为引用salaire_annee
  • 的主键的外键
  • 再次正向工程我的数据库
  • I first create my primary key in salaire_annee_ca,
  • Then Forward engineer my database
  • Declare my primary key as being a foreign key which references the primary key of salaire_annee
  • Forward engineer my database again

这篇关于错误1215:无法添加外键约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆