3

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

ERROR: Error 1215: Cannot add foreign key constraint

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 :

  • 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 :

These are the two main ones :


-- 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;

Any help would be appreciated!

4

4 回答 4

3

我在使用 Workbench 时遇到了可怕的错误 1215,并花了很长时间试图找出问题所在。我最终注意到我的一些表是用“latin1 - default collat​​ion”定义的,而另一些表是用“Schema Default”定义的。我不得不在 EER 图中一一展开表定义,才能看到更改它的选项。我将所有定义更改为“Schema Default”,问题就消失了。哇!

于 2016-01-11T15:56:47.250 回答
1

好吧,我想我明白了。mySql Workbench 似乎有问题,如果出现以下情况,错误就会消失:

  • 我首先在中创建我的主键salaire_annee_ca
  • 然后正向工程我的数据库
  • 将我的主键声明为引用主键的外键salaire_annee
  • 再次对我的数据库进行正向工程
于 2013-07-18T15:14:35.260 回答
1

错误得到解决:

为这样的表创建外键

CREATE TABLE users_so
  (
     username VARCHAR(10) NOT NULL,
     password VARCHAR(32) NOT NULL,
     enabled  SMALLINT,
     PRIMARY KEY (username)
  ); 

下面的代码工作正常

 CREATE TABLE authorities_so
  (
     username  VARCHAR(10) NOT NULL,
     authority VARCHAR(10) NOT NULL,
     FOREIGN KEY (username) REFERENCES users_so(username)
  );  
于 2015-04-15T12:49:24.380 回答
0

可能的原因之一可能是生产数据库上的默认存储引擎。

我的问题是相似的。我还使用 Workbench,并且在本地工作得很好,在生产中也工作得很好,直到我使用 Forward Engineering 在生产服务器上重新创建模式。

其中一张表成为MyISAM而不是InnoDb,并且Row formatDon't use成为Dynamic

我的解决方案是:

在工作台中,

  • 检查每个需要外键的表InnoDb,并且
  • Row format成为Default
于 2014-03-08T16:36:42.663 回答