MySQL的。无法创建架构。我收到此错误:ERROR 1005(HY000):(错误号:150)

时间:2015-07-22 20:14:34

标签: mysql schema ddl

我正在尝试创建一个简单的架构,请参阅下文。 但由于某种原因,我得到了这个奇怪的150错误。 我已经对架构进行了三重检查,我真的没有看到问题。 你能澄清我做错了什么吗?

DROP DATABASE IF EXISTS test222;
CREATE DATABASE IF NOT EXISTS test222 CHARSET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE test222;

CREATE TABLE im_savegroups (
    id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    sgcode VARCHAR(20) NOT NULL,
    PRIMARY KEY (id),
    UNIQUE KEY (sgcode)
) ENGINE=InnoDB CHARSET utf8mb4 COLLATE utf8mb4_unicode_ci;

CREATE TABLE im_savespecs (
    id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    fk_im_savegroups_sgcode VARCHAR(20) NOT NULL,
    sscode VARCHAR(20) NOT NULL,
    max_w INT UNSIGNED,
    max_h INT UNSIGNED,
    ratio_x INT UNSIGNED,
    ratio_y INT UNSIGNED,
    quality INT UNSIGNED,
    format VARCHAR(10),
    rel_dir VARCHAR(400),
    is_retina TINYINT UNSIGNED DEFAULT 0,
    is_preferred TINYINT UNSIGNED DEFAULT 0,
    PRIMARY KEY (id),
    FOREIGN KEY (fk_im_savegroups_sgcode) REFERENCES im_savegroups (sgcode) ON DELETE CASCADE ON UPDATE CASCADE,
    UNIQUE KEY (sscode)
) ENGINE=InnoDB CHARSET utf8mb4 COLLATE utf8mb4_unicode_ci;

CREATE TABLE im_originals (
    id INT UNSIGNED NOT NULL AUTO_INCREMENT,
    fk_im_savespecs_sscode VARCHAR(20) NOT NULL,
    name VARCHAR(255),
    alt VARCHAR(180),
    filename VARCHAR(64),
    caption VARCHAR(1024),
    credit VARCHAR(1024),
    expires_at DATETIME,
    created_at DATETIME,
    updated_at DATETIME,
    PRIMARY KEY (id),
    FOREIGN KEY (fk_im_savespecs_sscode) REFERENCES im_savespecs (sscode) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB CHARSET utf8mb4 COLLATE utf8mb4_unicode_ci;

这是我得到的实际错误:

ERROR 1005(HY000):无法创建表'test222.im_originals'(错误号:150)

1 个答案:

答案 0 :(得分:1)

删除此部分:

ON DELETE SET NULL

来自行:

FOREIGN KEY (fk_im_savespecs_sscode) REFERENCES im_savespecs (sscode) 
ON DELETE SET NULL ON UPDATE CASCADE
CREATE TABLE im_originals

解决了我的问题。

字段fk_im_savespecs_sscodeNOT NULL,因此在这种情况下,规则ON DELETE SET NULL似乎没有意义。