Search code examples
mysqlsqlmysql-workbenchmysql-error-1005

Mysql Forward engineer errno 150


I'm modeling my db shema using MySQL Workbench CE EER modeler and now I'm stuck with mysql errno 150.

My sql code:

CREATE  TABLE `myschema`.`Clients` (
  `phone` VARCHAR(15) NOT NULL ,
  `surname` VARCHAR(30) NOT NULL ,
  `name` VARCHAR(30) NOT NULL ,
  `middleName` VARCHAR(30) NULL ,
  `discountCardNumber` BIGINT NULL ,
  PRIMARY KEY (`phone`) ,
  UNIQUE INDEX `discountCardNumber_UNIQUE` (`discountCardNumber` ASC) ,
  CONSTRAINT `fk_Clients_DiscountCards1`
    FOREIGN KEY (`discountCardNumber` )
    REFERENCES `myschema`.`DiscountCards` (`cardNumber` )
    ON DELETE SET NULL
    ON UPDATE CASCADE)
ENGINE = InnoDB;

CREATE  TABLE `myschema`.`OrderStatuses` (
  `statusID` INT NOT NULL AUTO_INCREMENT ,
  `statusTitle` VARCHAR(45) NOT NULL ,
  `statusDescription` VARCHAR(150) NULL ,
  PRIMARY KEY (`statusID`) )
ENGINE = InnoDB;

CREATE  TABLE IF NOT EXISTS `myschema`.`WorkstationUsers` (
  `userID` INT NOT NULL AUTO_INCREMENT ,
  `login` VARCHAR(45) NOT NULL ,
  `pass` VARCHAR(45) NOT NULL ,
  `name` VARCHAR(45) NOT NULL ,
  `surname` VARCHAR(45) NOT NULL ,
  `middleName` VARCHAR(45) NULL ,
  PRIMARY KEY (`userID`) )
ENGINE = InnoDB;

CREATE TABLE `myschema`.`Orders` (
  `orderID` BIGINT NOT NULL AUTO_INCREMENT ,
  `registerDate` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
  `clientPhone` VARCHAR(15) NOT NULL ,
  `shipmentAddress` VARCHAR(150) NOT NULL ,
  `orderStatus` INT NOT NULL ,
  `registrator` INT NOT NULL ,
  PRIMARY KEY (`orderID`) ,
  INDEX `fk_Orders_Clients` (`clientPhone` ASC) ,
  INDEX `fk_Orders_OrderStatuses1` (`orderStatus` ASC) ,
  INDEX `fk_Orders_WorkstationUsers1` (`registrator` ASC) ,
  CONSTRAINT `fk_Orders_Clients`
    FOREIGN KEY (`clientPhone` )
    REFERENCES `myschema`.`Clients` (`phone` )
    ON DELETE SET NULL
    ON UPDATE CASCADE,
  CONSTRAINT `fk_Orders_OrderStatuses1`
    FOREIGN KEY (`orderStatus` )
    REFERENCES `myschema`.`OrderStatuses` (`statusID` )
    ON DELETE SET NULL
    ON UPDATE CASCADE,
  CONSTRAINT `fk_Orders_WorkstationUsers1`
    FOREIGN KEY (`registrator` )
    REFERENCES `myschema`.`WorkstationUsers` (`userID` )
    ON DELETE SET NULL
    ON UPDATE CASCADE)
ENGINE = InnoDB;

It fails in last statement (CREATE TABLE Orders).

I already checked:

  • Types of refferencing tables are equals
  • Indexes on all columns exists
  • Engine is InnoDB

Thanks for any help! Have a good day!

P.S. sorry for possible duplicate. I really can't find any problem in my code.


Solution

  • Some of fields are defined as NOT NULL, but you defined 'ON DELETE' action as 'SET NULL'.

    Make these fields nullabe -

    CREATE TABLE `myschema`.`Orders` (
      `orderID` BIGINT NOT NULL AUTO_INCREMENT ,
      `registerDate` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
      `clientPhone` VARCHAR(15) NULL, -- NOT NULL ,
      `shipmentAddress` VARCHAR(150) NOT NULL ,
      `orderStatus` INT NULL, -- NOT NULL ,
      `registrator` INT NULL, -- NOT NULL ,
      PRIMARY KEY (`orderID`) ,
      INDEX `fk_Orders_Clients` (`clientPhone` ASC) ,
      INDEX `fk_Orders_OrderStatuses1` (`orderStatus` ASC) ,
      INDEX `fk_Orders_WorkstationUsers1` (`registrator` ASC) ,
      CONSTRAINT `fk_Orders_Clients`
        FOREIGN KEY (`clientPhone` )
        REFERENCES `myschema`.`Clients` (`phone` )
        ON DELETE SET NULL
        ON UPDATE CASCADE,
      CONSTRAINT `fk_Orders_OrderStatuses1`
        FOREIGN KEY (`orderStatus` )
        REFERENCES `myschema`.`OrderStatuses` (`statusID` )
        ON DELETE SET NULL
        ON UPDATE CASCADE,
      CONSTRAINT `fk_Orders_WorkstationUsers1`
        FOREIGN KEY (`registrator` )
        REFERENCES `myschema`.`WorkstationUsers` (`userID` )
        ON DELETE SET NULL
        ON UPDATE CASCADE)
    ENGINE = InnoDB;