2012-09-29 43 views
2

因此,當我從mysql工作臺轉發工程模式時,出現了一個非常錯誤的150錯誤。在表格創建時MySQL 150的錯誤形式爲mysql workbench(外鍵選項與列中的非null衝突,FIXED)

我已經確定所有pk和fk都具有相同的類型。所有表都是innodb引擎。

也許我在這裏錯過了一些愚蠢的東西(並且道歉,如果我這樣做的話),但是您的幫助將不勝感激。

這裏是一個失敗,150錯誤的腳本:

SET @[email protected]@UNIQUE_CHECKS, UNIQUE_CHECKS=0; 
SET @[email protected]@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0; 
SET @[email protected]@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 [email protected]_SQL_MODE; 
SET [email protected]_FOREIGN_KEY_CHECKS; 
SET [email protected]_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 
+1

傢伙我把它整理出來! 如果它可能幫助我在這裏,我什麼是錯誤的: 在我的FK選項,我有SET NULL的刪除選項,而我有我的所有我的所有PK的NOT NULL。將刪除選項設置爲NO ACTION解決了問題。 – cmptrwizard

回答

0

users創建一個名爲entity_id的指數,並在同一時間名FK約束entity_id

我相信你不能有一個同名的索引和約束。

試試這個:

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