So, i'm getting a pretty 150 error when I forward engineer a schema from mysql workbench.
I have made sure all pk and fk have the same type. All tables are innodb engine.
Maybe I'm missing something silly here (and apologies if I did), but your help would be much appreciated.
Here is the script that fails with 150 error:
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL';
SHOW WARNINGS;
CREATE SCHEMA IF NOT EXISTS `bis` DEFAULT CHARACTER SET latin1 ;
SHOW WARNINGS;
USE `bis` ;
-- -----------------------------------------------------
-- Table `bis`.`entities`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `bis`.`entities` ;
SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `bis`.`entities` (
`entity_name` VARCHAR(64) NOT NULL ,
`entity_description` TEXT NULL DEFAULT NULL ,
`entity_email` TEXT NOT NULL ,
`entity_phone` VARCHAR(11) NULL DEFAULT NULL ,
`entity_website` VARCHAR(64) NULL DEFAULT NULL ,
PRIMARY KEY (`entity_name`) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;
SHOW WARNINGS;
-- -----------------------------------------------------
-- Table `bis`.`users`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `bis`.`users` ;
SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `bis`.`users` (
`user_name` VARCHAR(64) NOT NULL ,
`user_entity` VARCHAR(64) NOT NULL ,
`user_email` TEXT NOT NULL ,
`user_password` CHAR(64) NOT NULL ,
`user_salt` CHAR(64) NOT NULL ,
`user_role` VARCHAR(45) NOT NULL ,
`user_realName` VARCHAR(64) NOT NULL ,
`user_surname` VARCHAR(64) NOT NULL ,
`user_IDnumber` VARCHAR(13) NOT NULL ,
`user_cellNumber` VARCHAR(11) NOT NULL ,
PRIMARY KEY (`user_name`) ,
INDEX `entity_id` (`user_entity` ASC) ,
CONSTRAINT `entity_id`
FOREIGN KEY (`user_entity` )
REFERENCES `bis`.`entities` (`entity_name` )
ON DELETE SET NULL
ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;
SHOW WARNINGS;
-- -----------------------------------------------------
-- Table `bis`.`reports`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `bis`.`reports` ;
SHOW WARNINGS;
CREATE TABLE IF NOT EXISTS `bis`.`reports` (
`report_id` INT NOT NULL AUTO_INCREMENT ,
`user_name` VARCHAR(64) NOT NULL ,
`investigator_name` VARCHAR(64) NOT NULL ,
`report_entity` VARCHAR(64) NOT NULL ,
`report_finished` TINYINT(1) NULL DEFAULT false ,
`submit_date` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ,
`finished_date` TIMESTAMP NULL ,
`report_PDFFile` LONGBLOB NULL ,
PRIMARY KEY (`report_id`) ,
INDEX `user_id` (`user_name` ASC) ,
INDEX `investigator_id` (`investigator_name` ASC) ,
INDEX `entity_id` (`report_entity` ASC) ,
CONSTRAINT `user_id`
FOREIGN KEY (`user_name` )
REFERENCES `bis`.`users` (`user_name` )
ON DELETE SET NULL
ON UPDATE CASCADE,
CONSTRAINT `investigator_id`
FOREIGN KEY (`investigator_name` )
REFERENCES `bis`.`users` (`user_name` )
ON DELETE SET NULL
ON UPDATE CASCADE,
CONSTRAINT `entity_id`
FOREIGN KEY (`report_entity` )
REFERENCES `bis`.`entities` (`entity_name` )
ON DELETE SET NULL
ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1;
SHOW WARNINGS;
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
And then the corresponding error:
Executing SQL script in server
ERROR: Error 1005: Can't create table 'bis.users' (errno: 150)
CREATE TABLE IF NOT EXISTS `bis`.`users` (
`user_name` VARCHAR(64) NOT NULL ,
`user_entity` VARCHAR(64) NOT NULL ,
`user_email` TEXT NOT NULL ,
`user_password` CHAR(64) NOT NULL ,
`user_salt` CHAR(64) NOT NULL ,
`user_role` VARCHAR(45) NOT NULL ,
`user_realName` VARCHAR(64) NOT NULL ,
`user_surname` VARCHAR(64) NOT NULL ,
`user_IDnumber` VARCHAR(13) NOT NULL ,
`user_cellNumber` VARCHAR(11) NOT NULL ,
PRIMARY KEY (`user_name`) ,
INDEX `entity_id` (`user_entity` ASC) ,
CONSTRAINT `entity_id`
FOREIGN KEY (`user_entity` )
REFERENCES `bis`.`entities` (`entity_name` )
ON DELETE SET NULL
ON UPDATE CASCADE)
ENGINE = InnoDB
DEFAULT CHARACTER SET = latin1
SQL script execution finished: statements: 13 succeeded, 1 failed
The table users
creates an index named entity_id
and at the same time names the fk constraint entity_id
.
I believe you cannot have an index and constraint with the same name.
Try this:
INDEX entity_id (user_entity ASC) ,
CONSTRAINT fk_user_entity -- *** this is the change ***
FOREIGN KEY (user_entity )
REFERENCES bis.entities (entity_name )
ON DELETE SET NULL
ON UPDATE CASCADE
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.