MySQL error: Cannot add foreign key constraint (PrestaShop module)

MySQL error: Cannot add foreign key constraint (PrestaShop module)

我正在尝试为我的自定义 PrestaShop 模块编写 MySQL 查询。我在添加外键时遇到了问题,不知道哪里出了问题。我仔细检查了列类型及其存在。

这是错误:

Cannot add foreign key constraint

CREATE TABLE IF NOT EXISTS `ps_pp_project` (
    `id` INT NOT NULL AUTO_INCREMENT,
    `id_group` INT,
    `id_product` INT,
    `name` VARCHAR(255),
    `height` INT,
    `width` INT,
    `file` VARCHAR(255),
    `class_name` VARCHAR(255),
    PRIMARY KEY (`id`),
    FOREIGN KEY (`id_product`) REFERENCES `' ._DB_PREFIX_. '
    product`(`id_product`) ON DELETE CASCADE,
    FOREIGN KEY (`id_group`) REFERENCES `' ._DB_PREFIX_. 
    'pp_group`(`id`) ON DELETE CASCADE
) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=UTF8mb4';

这是我在安装过程中 运行 尝试的所有代码

    $sqls[] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'pp_group` (
    `id` INT NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(255) NULL,
    PRIMARY KEY (`id`)
) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=UTF8mb4';

$sqls[] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'pp_project` (
    `id` INT NOT NULL AUTO_INCREMENT,
    `id_group` INT,
    `id_product` INT(10),
    `name` VARCHAR(255),
    `height` INT,
    `width` INT,
    `file` VARCHAR(255),
    `class_name` VARCHAR(255),
    PRIMARY KEY (`id`),
    FOREIGN KEY (`id_product`) REFERENCES `' ._DB_PREFIX_. 'product`(`id_product`)
    ON DELETE CASCADE,
    FOREIGN KEY (`id_group`) REFERENCES `' ._DB_PREFIX_. 'pp_group`(`id`)
    ON DELETE CASCADE
) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=UTF8mb4';

$sqls[] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'pp_layer` (
    `id` INT NOT NULL AUTO_INCREMENT,
    `id_project` INT,
    `label` VARCHAR(255) DEFAULT \'Twój tekst\',
    `position_x` INT DEFAULT 0,
    `position_y` INT DEFAULT 0,
    `max_chars` INT DEFAULT 10,
    `text_align` VARCHAR(255) DEFAULT \'left\',
    `height` INT DEFAULT 0,
    `width` INT DEFAULT 0,
    `placeholder` VARCHAR(255) DEFAULT \'Tekst\',
    `rotation` INT DEFAULT 0,
    `skew_x` INT DEFAULT 0,
    `skew_y` INT DEFAULT 0,
    `color` VARCHAR(255) DEFAULT \'000\',
    `font_family` VARCHAR(255) DEFAULT \'arial\',
    `font_size` INT DEFAULT 12,
    `line_height` INT DEFAULT 12,
    `row_quantity` INT DEFAULT 1,
    `visible` BOOL DEFAULT 1,
    `layer_type` VARCHAR(255) DEFAULT \'text\',
    `file` VARCHAR(255) DEFAULT null,
    `opacity` FLOAT DEFAULT 1.0,
    PRIMARY KEY (`id`),
    FOREIGN KEY (`id_project`) REFERENCES `' ._DB_PREFIX_. 'pp_project`(`id`)
    ON DELETE CASCADE
) ENGINE=' . _MYSQL_ENGINE_ . ' DEFAULT CHARSET=UTF8mb4';


foreach ($sqls as $sql) {
    if (!Db::getInstance()->execute($sql)) {
        return false;
    }
}

查询在没有外键的情况下完美执行。

UPDATE

发现问题出在这一行:

FOREIGN KEY (`id_product`) REFERENCES `' ._DB_PREFIX_. '
product`(`id_product`) ON DELETE CASCADE,

但是,我还没有弄清楚如何修复它。

FOREIGN KEY (`id_group`) REFERENCES `pp_group`(`id`)

检查pp_group table是否存在,也许你可以写成ps_group或ps_pp_group。

我找到了问题的解决方案。

在 table ps_product 列中 id_productUNSIGNEDNOT NULL.

在创建 table ps_pp_project 时,我创建了列 id_product 而未指定这些属性

$sqls[] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'pp_project` (
`id` INT NOT NULL AUTO_INCREMENT,
`id_group` INT,
`id_product` INT(10),
[...]

要解决此问题,查询应如下所示:

$sqls[] = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'pp_project` (
`id` INT NOT NULL AUTO_INCREMENT,
`id_group` INT,
`id_product` INT(10) UNSIGNED NOT NULL,
[...]