我想添加到我的數據庫表,但似乎得到一個錯誤。我得到一個1452錯誤。以下是我認爲的問題:我認爲我的parent_fk指的是一個尚不存在的id。至少,我是這樣理解了以下錯誤:爲什麼我的insert語句失敗?
Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails (assetgallery.ag_asset, CONSTRAINT fk_ag_asset_2 FOREIGN KEY (parent_fk) REFERENCES ag_asset (id) ON DELETE CASCADE ON UPDATE CASCADE)
說我有我的數據庫中的元組id爲2的第一個元組:
'2', '8', NULL, NULL, '2', '2015-09-24 09:42:31', 'sabernLogo_0.png', NULL, NULL, NULL, '180', '80', NULL, '1', NULL, NULL, '3', '1'
我想補充另一個與INSERT
聲明。我插入沒有ID,所以它看起來是這樣的:
Insert into ag_asset
(album_fk, parent_fk, head_fk, status_fk, modified, filename, title, `desc`, `text`, width, height, owner_fk, locked, remarks, group_fk, user_fk, type_fk)
VALUES(6, 0, null, 1, '2015-10-15 15:47:13.0', 'index.png', null, null, null, 215, 234, null, null, null, null, 3, 1);
據我瞭解,因爲我的ID是Autoincrement
「版,不填充這將導致新鮮的ID,爲2。 這裏創建聲明
CREATE TABLE `ag_asset` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`album_fk` int(10) DEFAULT NULL,
`parent_fk` int(10) DEFAULT NULL,
`head_fk` int(10) DEFAULT NULL,
`status_fk` int(10) DEFAULT '1',
`modified` datetime NOT NULL,
`filename` varchar(255) DEFAULT NULL,
`title` varchar(255) DEFAULT NULL,
`desc` text,
`text` longtext,
`width` int(10) DEFAULT NULL,
`height` int(10) DEFAULT NULL,
`owner_fk` int(10) DEFAULT NULL,
`locked` int(10) DEFAULT NULL,
`remarks` text,
`group_fk` int(10) DEFAULT NULL,
`user_fk` int(10) DEFAULT NULL,
`type_fk` int(10) DEFAULT '1',
PRIMARY KEY (`id`),
KEY `in_filename` (`filename`(12)),
KEY `in_title` (`title`(12)),
KEY `fk_ag_asset_1` (`album_fk`),
KEY `fk_ag_asset_2` (`parent_fk`),
KEY `fk_ag_asset_3` (`head_fk`),
KEY `fk_ag_asset_4` (`status_fk`),
KEY `fk_ag_asset_5` (`owner_fk`),
KEY `fk_ag_asset_6` (`type_fk`),
CONSTRAINT `fk_ag_asset_1` FOREIGN KEY (`album_fk`) REFERENCES `ag_album` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_ag_asset_2` FOREIGN KEY (`parent_fk`) REFERENCES `ag_asset` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_ag_asset_3` FOREIGN KEY (`head_fk`) REFERENCES `ag_asset` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
CONSTRAINT `fk_ag_asset_4` FOREIGN KEY (`status_fk`) REFERENCES `ag_status` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_ag_asset_5` FOREIGN KEY (`owner_fk`) REFERENCES `ag_owner` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `fk_ag_asset_6` FOREIGN KEY (`type_fk`) REFERENCES `ag_type` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=31 DEFAULT CHARSET=utf8;
我是否正確理解錯誤或者我是否在錯誤的方向查找?
你是對的。 'parent_fk'設置爲'0'。嘗試將其更改爲NULL。即'VALUES(6,null,null,...' – AmazingDreams