错误 1005 (HY000): 无法创建表......\issue.frm' (错误号:150)
这是 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,
代码就可以了。
This is the 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;
I got this error message from the above code:
ERROR 1005 (HY000): Can't create table '......\issue.frm' (errno: 150)
However, if I change
FOREIGN KEY (duplicateOf) REFERENCES issue (issueID)
ON DELETE SET NULL,
to
FOREIGN KEY (duplicateOf) REFERENCES issue (issueID)
ON DELETE NO ACTION,
the code works.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这里的问题是您在问题表主键定义中指定列,以便在其父行被删除时将其设置为空。 MySQL 不会喜欢这样,因为主键列不允许包含空值。
对问题表的 DDL 进行快速调整应该可以让您做您想做的事情。主键和唯一键之间的关键(无双关语)区别之一是唯一键列允许包含空值。我猜测 issuesID 列也将是唯一的,因为它被指定为 AUTO_INCRMENT。尝试以下操作:
祝你好运!
I reckon the problem here is that you are specifying columns in your issue table primary key definition to be set to null in the event of their parent row being deleted. MySQL will not like this since primary key columns are not allowed to contain null values.
A quick tweak to the DDL of the issue table should allow you to do what you want. One of the key (no pun intended) differences between a primary key and a unique key is that unique key columns are allowed to contain null values. I'm taking a guess that the issueID column will be unique too given that it is specified as AUTO_INCREMENT. Try the following:
Good luck!