我有一个称为邮递的表格,有一个外国关键栏被删除。 用户用pk表列出参考资料。 当我创建新的邮件时,我想把这个领域划为无效,因为邮件被删除,然而,当我确实遇到外国关键制约因素失败错误时。
我完全理解这一错误,但我如何绕过这一错误?
表:-
CREATE TABLE `mail` (
`pk` int(10) unsigned NOT NULL AUTO_INCREMENT,
`from` int(11) unsigned NOT NULL,
`to` int(11) unsigned NOT NULL,
`subject` varchar(255) NOT NULL,
`message` text NOT NULL,
`dt` datetime NOT NULL,
`read` enum( 0 , 1 ) NOT NULL DEFAULT 0 ,
`deletedby` int(11) unsigned DEFAULT 0 ,
PRIMARY KEY (`pk`),
KEY `from_mail__users_pk` (`from`),
KEY `to_mail__users_pk` (`to`),
KEY `deletedby_mail__users_pk` (`deletedby`),
CONSTRAINT `deletedby_mail__users_pk` FOREIGN KEY (`deletedby`) REFERENCES `user` (`pk`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `from_mail__users_pk` FOREIGN KEY (`from`) REFERENCES `user` (`pk`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `to_mail__users_pk` FOREIGN KEY (`to`) REFERENCES `user` (`pk`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 COLLATE=utf8_bin
错误信息:
ERROR 1452: Cannot add or update a child row: a foreign key constraint fails (`ajfit`.`mail`, CONSTRAINT `deletedby_mail__users_pk` FOREIGN KEY (`deletedby`) REFERENCES `user` (`pk`) ON DELETE NO ACTION ON UPDATE NO ACTION)
SQL Statement:
INSERT INTO `ajfit`.`mail` (`from`, `to`, `subject`, `message`, `read`) VALUES (31, 30, test , test , 0 )
成就
纽约总部