2

因此,当我从 mysql 工作台对模式进行转发工程时,我收到了 150 个错误。

我确保所有 pk 和 fk 都具有相同的类型。所有表都是innodb引擎。

也许我在这里遗漏了一些愚蠢的东西(如果我这样做了,我会道歉),但您的帮助将不胜感激。

这是失败并出现 150 错误的脚本:

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;

然后是相应的错误:

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
4

1 回答 1

0

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
于 2012-09-29T09:53:42.350 回答