开发者

MySQL Not allowing delete even though FK relationship ON DELETE CASCADE set

I am working on a project that makes use of a MySQL Database to store snippets of code for use on multiple websites. For each content snippet I also keep an edit history table, to which I add a record every time a snippet is updated. Occasionally it will be desirable to delete a snippet completely, and any associated edit history. When setting up the DB, I set up the foreign key relationship to ON DELETE CASCADE so that deleting the snippet will automatically delete the history. However, I am getting the following error:

Error in query: delete from SNIPPET where id = 1. Cannot delete or update a parent row: a foreign key constraint fails (universal_content_repository/SNIPPET_EDIT_HISTORY, CONSTRAINT fk_SNIPPET_EDIT_HISTORYRelationship13 FOREIGN KEY (snippet_id) REFERENCES SNIPPET (id))

Here is the code I use to create the DB as well as the relationships:

/*Schema universal_content_repository*/
CREATE SCHEMA IF NOT EXISTS `universal_content_repository`
DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;

USE `universal_content_repository`;

CREATE TABLE `universal_content_repository`.`USER` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'Stores the ID for the User.',
`username` VARCHAR(20) NOT NULL, 
`first_name` VARCHAR(32) NOT NULL,
`last_name` VARCHAR(32) NOT NULL,
`is_active` VARCHAR(5) NOT NULL DEFAULT true,
`password` VARCHAR(32) NOT NULL,
`is_admin` BIT NOT NULL DEFAULT 0,
`prefers_wysiwyg` BIT DEFAULT 0,
PRIMARY KEY (`id`)
) COMMENT 'Stores information about all Users for the Universal Content Repository.' ENGINE=INNODB
ROW_FORMAT=DEFAULT;

CREATE TABLE `universal_content_repository`.`SNIPPET` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`title` VARCHAR(255) NOT NULL,
`content` TEXT NOT NULL,
`created_by` INT UNSIGNED,
`wysiwyg_editable` VARCHAR(6) NOT NULL DEFAULT true,
`is_enabled` BIT NOT NULL DEFAULT 1,
PRIMARY KEY (`id`)
) COMMENT 'Guarantees that no two snippets may have the same name or ID.' ENGINE=INNODB
ROW_FORMAT=DEFAULT;

CREATE TABLE `universal_content_repository`.`IMAGE` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`name` VARCHAR(32) NOT NULL,
`url` TEXT NOT NULL,
`alt` VARCHAR(32),
PRIMARY KEY (`id`)
) ENGINE=INNODB
ROW_FORMAT=DEFAULT;

CREATE TABLE `universal_content_repository`.`IMAGE_IN_SNIPPET` (
`rel_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`snippet_id` INT UNSIGNED,
`image_id` INT UNSIGNED,
`position` INT COMMENT 'Stores the position of the image within the snippet, as notated in the snippet as [index]',
PRIMARY KEY (`rel_id`)
) ENGINE=INNODB
ROW_FORMAT=DEFAULT;

CREATE TABLE `universal_content_repository`.`SNIPPET_EDIT_HISTORY` (
`revision_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`editing_user` INT UNSIGNED,
`snippet_id` INT UNSIGNED,
`old_contents` TEXT NOT NULL COMMENT 'Stores the old contents of the snippet.',
`edit_date` DATETIME NOT NULL COMMENT 'Stores the DateTime of the edit.',
PRIMARY KEY (`revision_id`)
) ENGINE=INNODB
ROW_FORMAT=DEFAULT;

CREATE TABLE `universal_content_repository`.`SESSION` (
`id` VARCHAR(32) NOT NULL COMMENT 'Stores the Session ID',
`access` INT(10) UNSIGNED NOT NULL,
`data` TEXT,
PRIMARY KEY (`id`)
) ENGINE=INNODB
ROW_FORMAT=DEFAULT;

ALTER TABLE `universal_content_repository`.`USER` ADD UNIQUE `Identifiers` (`id`,`username`);

ALTER TABLE `universal_content_repository`.`SNIPPET` ADD UNIQUE `identifiers` (`title`,`id`);

ALTER TABLE `universal_content_repository`.`SNIPPET` ADD CONSTRAINT `fk_SNIPPETRelationship8` FOREIGN KEY (`created_by`) REFERENCES `universal_content_repository`.`USER`(`id`) MATCH SIMPLE ON UPDATE RESTRICT ON DELETE RESTRICT;

ALTER TABLE `universal_content_repository`.`IMAGE_IN_SNIPPET` ADD CONSTRAINT `fk_IMAGE_IN_SNIPPETRelationship10` FOREIGN KEY (`snippet_id`) REFERENCES `universal_content_repository`.`SNIPPET`(`id`) MATCH SIMPLE ON UPDATE CASCADE ON DELETE CASCADE;

ALTER TABLE `universal_content_repository`.`IMAGE_IN_SNIPPET` ADD CONSTRAINT `fk_IMAGE_IN_SNIPPETRelationship11` FOREIGN KEY (`image_id`) REFERENCES `universal_content_repository`.`IMAGE`(`id`) MATCH SIMPLE ON UPDATE RESTRICT ON DELETE RESTRICT;

ALTER TABLE `universal_content_repository`.`SNIPPET_EDIT_HISTORY` ADD CONSTRAINT `fk_SNIPPET_EDIT_HISTORYRelationship12` F开发者_运维问答OREIGN KEY (`editing_user`) REFERENCES `universal_content_repository`.`USER`(`id`) MATCH SIMPLE ON UPDATE RESTRICT ON DELETE RESTRICT;

ALTER TABLE `universal_content_repository`.`SNIPPET_EDIT_HISTORY` ADD CONSTRAINT `fk_SNIPPET_EDIT_HISTORYRelationship13` FOREIGN KEY (`snippet_id`) REFERENCES `universal_content_repository`.`SNIPPET`(`id`) MATCH SIMPLE ON UPDATE CASCADE ON DELETE CASCADE;

If you want to see a graphical representation of the DB, you can see it at SchemaBank. For those without a SchemaBank account, here is the ER:

MySQL Not allowing delete even though FK relationship ON DELETE CASCADE set

Any ideas?


Looks like your code is right.

If it's practical, dump that database and restore it onto a different server as a test. If INNODB and MySQL internal states have gotten out of sync, that should give you a well-behaved database on the server you restore to.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜