mysql. can't create schema. I get this error: ERROR 1005 (HY000): (errno: 150)

mysql. can't create schema. I get this error: ERROR 1005 (HY000): (errno: 150)

我正在尝试创建一个简单的架构,请参见下文。 但出于某种原因,我得到了这个奇怪的 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;

这是我得到的实际错误:

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

删除这部分:

ON DELETE SET NULL

来自行:

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

of CREATE TABLE im_originals 解决了我这边的问题。

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