Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL Foreign key constraint are incompatible

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;
like image 760
Karaja Avatar asked Mar 12 '26 20:03

Karaja


1 Answers

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)

like image 135
Karaja Avatar answered Mar 16 '26 04:03

Karaja