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.
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).
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
Related
when I wanted to generate tables, I got this error I don't know how to fix it. Is anyone face to this problem and how you solve it?
thanks
CREATE TABLE IF NOT EXISTS `tb_inv_detail` (
`inv_id_fk` INT UNSIGNED NOT NULL,
`part_id_fk` INT UNSIGNED NOT NULL,
`qty` DECIMAL(12,2) NOT NULL,
`tb_detailed` VARCHAR(50) NULL,
`tb_inv_detailcol` VARCHAR(45) NULL,
PRIMARY KEY (`inv_id_fk`),
INDEX `fk_tb_inv_detail_tb_parts1_idx` (`part_id_fk` ASC) VISIBLE,
CONSTRAINT `fk_tb_inv_detail_tb_parts1`
FOREIGN KEY (`part_id_fk`)
REFERENCES `tb_parts` (`part_id_pk`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_tb_inv_detail_tb_invoice1`
FOREIGN KEY (`inv_id_fk`)
REFERENCES `tb_invoice` (`inv_id_pk`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 25 succeeded, 1 failed
Also this screenshot of my EER diagram:
Well the obvious code which is missing from what you posted above is the DDL for the two tables referred to by the foreign keys defined in tb_inv_detail. These tables are:
tb_parts
tb_invoice
These tables must be defined first, with correctly named primary key columns to match the foreign keys in your table above.
CREATE TABLE tb_parts (
part_id_pk INT UNSIGNED NOT NULL,
PRIMARY KEY (part_id_pk), -- referred to by part_id_fk
...
)
CREATE TABLE tb_invoice (
inv_id_pk INT UNSIGNED NOT NULL,
PRIMARY KEY (inv_id_pk), -- referred to by inv_id_fk
...
)
MySQL Version 8.0.17
The full error reads:
Referencing column 'groupLineId' and referenced column 'groupLineId' in foreign key constraint 'salesItemLine-groupLine' are incompatible
I am trying to link two tables via the groupLineId which are both NOT NULL VARCHAR(12). I am not sure why I am getting the error. I have several other foreign key relationships like this in my DB.
I am using the following code to generate the two tables. (Note: code for invoice table not shown)
CREATE TABLE IF NOT EXISTS `reports`.`groupLine` (
`groupLineId` VARCHAR(12) NOT NULL,
`lineNum` INT NOT NULL,
`invoiceId` VARCHAR(12) NOT NULL,
PRIMARY KEY (`groupLineId`, `lineNum`, `invoiceId`),
INDEX `groupLine-invoice_idx` (`invoiceId` ASC) VISIBLE,
CONSTRAINT `groupLine-invoice`
FOREIGN KEY (`invoiceId`)
REFERENCES `reports`.`invoice` (`invoiceId`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `reports`.`salesItemLine` (
`groupLineId` VARCHAR(12) NOT NULL,
`lineNum` INT NOT NULL,
`description` VARCHAR(256) NULL,
`amount` DECIMAL NULL,
`detailType` VARCHAR(45) NULL,
PRIMARY KEY (`groupLineId`, `lineNum`),
INDEX `salesItemLine-groupLine_idx` (`groupLineId` ASC) VISIBLE,
CONSTRAINT `salesItemLine-groupLine`
FOREIGN KEY (`groupLineId`)
REFERENCES `reports`.`groupLine` (`groupLineId`)
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
If anyone comes across this my problem was how I was making changes and using the "Forward Engineer" feature of MySQL Workbench. I had originally created the database with groupLineId ID as type INT. I then changed the model to make the groupLineId in both tables to VARCHAR(12). Then when I would run "Forward Engineer" it would first write the new groupLine table and change the type of groupLineId to VARCHAR(12) this would then break the existing FROGIEN key with salesItemLine table which has not been updated and still has the type of groupeLineId as INT.
The solution was to DROP both tables before rerunning the forward engineering. (Or at least manually dropping the existing constraints)
So this is my code, I cannot proceed because I am always getting that foreign key constraint error. I have checked the data type they are consistent. Where am I going wrong?. All the problem persists where foreign keys are required. i have just posted two tables here.
-- -----------------------------------------------------
-- Table `44376936`.`AccountType`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `44376936`.`AccountType` ;
CREATE TABLE IF NOT EXISTS `44376936`.`AccountType` (
`AccountTypeID` float NOT NULL,
`AccountTypeName` VARCHAR(45) NULL,
`AccountTypeDesc` VARCHAR(45) NULL,
`AccountTypeInterestRate` FLOAT NULL,
`AccountTypeServiceFee` FLOAT NULL,
PRIMARY KEY (`AccountTypeID`))
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `44376936`.`Account`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `44376936`.`Account` ;
CREATE TABLE IF NOT EXISTS `44376936`.`Account` (
`AccountBSB` INT NOT NULL,
`AccountNumber` INT NOT NULL,
`AccountCurrentBalance` FLOAT NULL,
`AccountType_AccountTypeID` Float NOT NULL,
PRIMARY KEY (`AccountBSB`, `AccountNumber`, `AccountType_AccountTypeID`),
INDEX `fk_Account_AccountType_idx` (`AccountType_AccountTypeID` ASC),
CONSTRAINT `fk_Account_AccountType`
FOREIGN KEY (`AccountType_AccountTypeID`)
REFERENCES `44376936`.`AccountType` (`AccountTypeID`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB;
I can reproduce
It comes from this instruction :
REFERENCES `44376936`.`AccountType` (`AccountTypeID`)
and the problem is the database name, probably because it begins with a number and not a letter.
This works :
REFERENCES `AccountType` (`AccountTypeID`)
So get rid of the database name. If you are running this without being on the 44376936 database, execute this instruction at the beginning of your script :
USE `44376936`;
Rextester example
Executing SQL script in server
ERROR: Error 1022: Can't write; duplicate key in table 'dependent'
SQL Code:
-- -----------------------------------------------------
-- Table `mydb`.`DEPENDENT`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `mydb`.`DEPENDENT` (
`Essn` CHAR(9) NOT NULL,
`Dependent_name` VARCHAR(45) NOT NULL,
`Sex` CHAR NULL,
`Bdate` DATE NULL,
`Relationship` VARCHAR(45) NULL,
PRIMARY KEY (`Dependent_name`, `Essn`),
CONSTRAINT `Essn`
FOREIGN KEY (`Essn`)
REFERENCES `mydb`.`EMPLOYEE` (`Ssn`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 10 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch`enter code here`
Am getting this error. Can some one help me on this. Thanks
hi you have primary key defined on the Dependent_name if you try to insert same name 2 times you will get error So you have to define other primary key if you expect duplicate name a common approach id to use auto increment column as primary key but you can define composite keys as well for primary key
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!
I hit the dreaded error 1215 while using the Workbench and spent a long time trying to work out what was wrong. I eventually noticed that some of my tables were defined with "latin1 - default collation", while others were defined with "Schema Default". I had to expand the table definitions one by one in the EER diagram to see the option to change this. I changed all the definitions to "Schema Default" and the problem disappeared. Wow!
Ok I think I figured it out. It seems to be a problem with mySql Workbench, the error disappears if :
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
The error gets resolved:
To create a Foreign key for a table like this
CREATE TABLE users_so
(
username VARCHAR(10) NOT NULL,
password VARCHAR(32) NOT NULL,
enabled SMALLINT,
PRIMARY KEY (username)
);
The below code works fine
CREATE TABLE authorities_so
(
username VARCHAR(10) NOT NULL,
authority VARCHAR(10) NOT NULL,
FOREIGN KEY (username) REFERENCES users_so(username)
);
One of possible cause could be default storage engine on production db.
My problem was similar. I also worked with Workbench, and in local worked perfectly, also worked good in production, until I recreate schema on production server with Forward Engineering.
One of tables become MyISAM instead of InnoDb, and Row format from Don't use become Dynamic.
My solution was:
In Workbench,
Check every table that requires foreign keys to be InnoDb, and
Row format to be Default.