我有一个名为 mail 的表,其中包含一个名为 deletedBy 的外键列,它通过 pk 引用 users 表。当我创建新邮件时,我想将此字段设置为 null,因为邮件未删除,但是当我这样做时,我收到外键约束失败错误。
我完全理解这个错误,但我该如何解决呢?
桌子:-
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')
谢谢
安德鲁