SQLError - 创建外键

问题描述

尝试添加外键时出错

SQLError

在此之前,执行了以下操作,安装了索引

maintable

subordinatetable

按照https://folkprog.net/sozdanie-svyazey-phpmyadmin/行事。

错误文本:

ALTER TABLE monoblocks ADD FOREIGN KEY (image_id) REFERENCES monoblock_images(id) ON DELETE RESTRICT ON UPDATE RESTRICT;

#1452 - 无法添加或更新子行:外键约束失败 (myishop.#sql-2150_93,CONSTRAINT #sql-2150_93_ibfk_21 FOREIGN KEY (image_id) REFERENCES {{ 1}} (monoblock_images))

我找到了

id

CREATE TABLE `monoblocks` (
 `id` int(11) NOT NULL AUTO_INCREMENT,`goods_id` int(11) NOT NULL,`screendiagonal_id` int(11) NOT NULL,`cpu_id` int(11) NOT NULL,`ramsize_id` int(11) NOT NULL,`os_id` int(11) NOT NULL,`hdd_id` int(11) NOT NULL,`screenresolution_id` int(11) NOT NULL,`matrixtype_id` int(11) NOT NULL,`countcpucores` int(11) NOT NULL,`videocards_id` int(11) NOT NULL,`videocardtypes_id` int(11) NOT NULL,`frequency_id` int(11) NOT NULL,`ramtype_id` int(11) NOT NULL,`oddtype_id` int(11) NOT NULL,`bodycolor_id` int(11) NOT NULL,`weight` int(11) NOT NULL,`wifisupport_id` int(11) NOT NULL,`bluetoothsupport_id` int(11) NOT NULL,`wifistandard_id` int(11) NOT NULL,`bluetoothstandard_id` int(11) NOT NULL,`guarantee_id` int(11) NOT NULL,`connectorsnumberusb2.0` int(11) NOT NULL,`connectorsnumberusb3.0` int(11) NOT NULL,`connectorsnumberhdmi` int(11) NOT NULL,`microphoneconnecors` int(11) NOT NULL,`headphoneconnectors` int(11) NOT NULL,`builtinmicrophone` tinyint(1) NOT NULL,`builtinspeakers` tinyint(1) NOT NULL,`touchscreen` tinyint(1) NOT NULL,`webcam` tinyint(1) NOT NULL,`keyboard_included` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,`mouse_included` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,`ssd_volume` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,`dimensions` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,`image_id` int(11) DEFAULT NULL,PRIMARY KEY (`id`),UNIQUE KEY `id` (`id`),KEY `goods_id` (`goods_id`),KEY `screendiagonal_id` (`screendiagonal_id`),KEY `cpu_id` (`cpu_id`),KEY `ramsize_id` (`ramsize_id`),KEY `os_id` (`os_id`),KEY `hdd` (`hdd_id`),KEY `screenresolution_id` (`screenresolution_id`),KEY `matrixtype_id` (`matrixtype_id`),KEY `countcpucores` (`countcpucores`),KEY `videocards_id` (`videocards_id`),KEY `videocardtypes_id` (`videocardtypes_id`),KEY `frequency_id` (`frequency_id`),KEY `ramtype_id` (`ramtype_id`),KEY `oddtype_id` (`oddtype_id`),KEY `bodycolor_id` (`bodycolor_id`),KEY `wifisupport_id` (`wifisupport_id`),KEY `bluetoothsupport_id` (`bluetoothsupport_id`),KEY `wifistandard_id` (`wifistandard_id`),KEY `bluetoothstandard_id` (`bluetoothstandard_id`),KEY `guarantee_id` (`guarantee_id`),KEY `image_id` (`image_id`),CONSTRAINT `monoblocks_ibfk_1` FOREIGN KEY (`goods_id`) REFERENCES `goods` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,CONSTRAINT `monoblocks_ibfk_10` FOREIGN KEY (`videocards_id`) REFERENCES `videocards` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,CONSTRAINT `monoblocks_ibfk_11` FOREIGN KEY (`videocardtypes_id`) REFERENCES `videocardtypes` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,CONSTRAINT `monoblocks_ibfk_12` FOREIGN KEY (`frequency_id`) REFERENCES `cpufrequency` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,CONSTRAINT `monoblocks_ibfk_13` FOREIGN KEY (`ramtype_id`) REFERENCES `rammemory` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,CONSTRAINT `monoblocks_ibfk_14` FOREIGN KEY (`oddtype_id`) REFERENCES `oddtype` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,CONSTRAINT `monoblocks_ibfk_15` FOREIGN KEY (`bodycolor_id`) REFERENCES `mainbodycolor` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,CONSTRAINT `monoblocks_ibfk_16` FOREIGN KEY (`wifisupport_id`) REFERENCES `wifisupport` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,CONSTRAINT `monoblocks_ibfk_17` FOREIGN KEY (`bluetoothsupport_id`) REFERENCES `bluetoothsupport` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,CONSTRAINT `monoblocks_ibfk_18` FOREIGN KEY (`wifistandard_id`) REFERENCES `wifistandard` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,CONSTRAINT `monoblocks_ibfk_19` FOREIGN KEY (`bluetoothstandard_id`) REFERENCES `bluetoothstandard` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,CONSTRAINT `monoblocks_ibfk_2` FOREIGN KEY (`screendiagonal_id`) REFERENCES `screendiagonal` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,CONSTRAINT `monoblocks_ibfk_20` FOREIGN KEY (`guarantee_id`) REFERENCES `guarantee` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,CONSTRAINT `monoblocks_ibfk_3` FOREIGN KEY (`cpu_id`) REFERENCES `cpus` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,CONSTRAINT `monoblocks_ibfk_4` FOREIGN KEY (`ramsize_id`) REFERENCES `ramsize` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,CONSTRAINT `monoblocks_ibfk_5` FOREIGN KEY (`os_id`) REFERENCES `os` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,CONSTRAINT `monoblocks_ibfk_6` FOREIGN KEY (`hdd_id`) REFERENCES `disksandrives` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,CONSTRAINT `monoblocks_ibfk_7` FOREIGN KEY (`screenresolution_id`) REFERENCES `screenresolution` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,CONSTRAINT `monoblocks_ibfk_8` FOREIGN KEY (`matrixtype_id`) REFERENCES `matrixtype` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,CONSTRAINT `monoblocks_ibfk_9` FOREIGN KEY (`countcpucores`) REFERENCES `numbercpucores` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

(可能有助于回答问题)

CREATE TABLE `monoblock_images` ( `id` int(11) NOT NULL AUTO_INCREMENT,`path_to_image` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL,PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci 上的回答中,我给出了

monoblockquery

解决方法

image_id 在表 monoblocks 中可以为 NULL,但在其他表中不能。