Question

I have been using mySQL to create a database but when I try to forward engineer my EER Diagram the database keeps sending me back the same error I have tried multiple fixes does anyone see what the problem is? The error code is 1215

Executing SQL script in server
ERROR: Error 1215: Cannot add foreign key constraint

SQL Code:

    -- -----------------------------------------------------
    -- Table `mydb`.`Employee`
    -- -----------------------------------------------------
    CREATE TABLE IF NOT EXISTS `mydb`.`Employee` (
      `EID` INT NOT NULL,
      `Fname` VARCHAR(45) NOT NULL,
      `Lname` VARCHAR(45) NOT NULL,
      `AddressID` INT NOT NULL,
      `PayLevel` FLOAT NOT NULL,
      `Jobtitle` VARCHAR(45) NOT NULL,
      `Date of Employment` DATE NOT NULL,
      PRIMARY KEY (`EID`),
      CONSTRAINT `fk_Employee_Store1`
        FOREIGN KEY (`EID`)
        REFERENCES `mydb`.`Store` (`EID`)
        ON DELETE NO ACTION
        ON UPDATE NO ACTION)
    ENGINE = InnoDB

SQL script execution finished: statements: 8 succeeded, 1 failed

Fetching back view definitions in final form. Nothing to fetch

This is the parent code, multiple times I have tried switching the relationships however employee9the top code) should be a child of Store.

    -- -----------------------------------------------------
-- Table `mydb`.`Store`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`Store` (
  `SID` INT NOT NULL,
  `StoreName` VARCHAR(45) NULL,
  `AddressID` INT NULL,
  `EID` INT NOT NULL,
  `CID` INT NULL,
  `MID` INT NULL,
  PRIMARY KEY (`SID`, `EID`),
  INDEX `fk_Store_Employee1_idx` (`EID` ASC),
  CONSTRAINT `fk_Store_Employee1`
    FOREIGN KEY (`EID`)
    REFERENCES `mydb`.`Employee` (`EID`)
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB;

Does anyone see the problem, Btw I have checked the type's multiple times they are equivalent.

Was it helpful?

Solution

you are trying to create a foreign key in store from employee and another one in employee from store. if this is a parent/child relation then only the primary key from the parent that is used in the child. if you have n<->n relation then you need a new table that holds both foreign keys. if I understand, here you need to have a new table (work) with (SID, EID) where both point to their respective tables (store and employee). Also creation order is important (parents first then child tables).

OTHER TIPS

You have a foreign key constraint operating in both directions. When you're creating the tables the first to be created will fail because the second doesn't exist. I'm not even sure that MySQL will accept a circular reference like this. You should probably remove the foreign key constraint applied to mydb.store

However, if this is essential you can ask MySQL to ignore the foreign key checks while you create the table. Just execute

SET foreign_key_checks = 0;

before you create the tables and

SET foreign_key_checks = 1;

after you finish

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top