ERROR 1005(HY000):无法创建表'...... \ issue.frm'(错误号:150)

时间:2011-09-27 15:39:10

标签: mysql sql foreign-keys innodb

这是SQL:

CREATE TABLE user (
    userID INTEGER UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL,
    username VARCHAR(100) NOT NULL,
    isAdmin BOOL NOT NULL DEFAULT 0,
    canAssignIssue BOOL NOT NULL DEFAULT 0,
    canMarkDuplicate BOOL NOT NULL DEFAULT 0,
    canProcessIssue BOOL NOT NULL DEFAULT 0
) ENGINE = InnoDB;

CREATE TABLE issue (
    issueID INTEGER UNSIGNED AUTO_INCREMENT NOT NULL,
    title VARCHAR(100) NOT NULL,
    body TEXT NOT NULL,
    duplicateOf INTEGER UNSIGNED DEFAULT NULL,
    issueDateTime DATETIME NOT NULL,
    postBy INTEGER UNSIGNED NOT NULL,
    PRIMARY KEY (issueID, postBy, duplicateOf),
    INDEX (postBy, duplicateOf), 
    FOREIGN KEY (duplicateOf) REFERENCES issue (issueID)
        ON DELETE SET NULL,
    FOREIGN KEY (postBy) REFERENCES user (userID)
        ON DELETE SET NULL
) ENGINE = InnoDB;

我从上面的代码中收到了此错误消息:

ERROR 1005 (HY000): Can't create table '......\issue.frm' (errno: 150)

但是,如果我改变

    FOREIGN KEY (duplicateOf) REFERENCES issue (issueID)
        ON DELETE SET NULL,

    FOREIGN KEY (duplicateOf) REFERENCES issue (issueID)
        ON DELETE NO ACTION,

代码有效。

1 个答案:

答案 0 :(得分:2)

我认为这里的问题是,您要将问题表主键定义中的列指定为在其父行被删除时设置为null。 MySQL不会喜欢这个,因为不允许主键列包含空值。

快速调整问题表的DDL应该可以让你做你想做的事情。主键和唯一键之间的关键(没有双关语)差异之一是允许唯一键列包含空值。我猜测issID列也是唯一的,因为它被指定为AUTO_INCREMENT。请尝试以下方法:

CREATE TABLE issue (
issueID INTEGER UNSIGNED AUTO_INCREMENT NOT NULL,
title VARCHAR(100) NOT NULL,
body TEXT NOT NULL,    
duplicateOf INTEGER UNSIGNED,
issueDateTime DATETIME NOT NULL,
postBy INTEGER UNSIGNED NULL,
PRIMARY KEY (issueID),
UNIQUE INDEX (issueID,duplicateOf,postBy),
INDEX (postBy, duplicateOf), 
FOREIGN KEY (duplicateOf) REFERENCES issue (issueID)
ON DELETE SET NULL,
FOREIGN KEY (postBy) REFERENCES user (userID)
ON DELETE SET NULL)
ENGINE = InnoDB;
祝你好运!