【Python】mysql添加外键报错: 1452 Cannot add or update a child row: a foreign key constraint fails

Python执行程序报错,如下:

IntegrityError at /admin/accounts/profile/1/change/
(1452, 'Cannot add or update a child row: a foreign key constraint fails (`trop_django`.`django_admin_log`, CONSTRAINT `django_admin_log_user_id_c564eba6_fk_auth_user_id` FOREIGN KEY (`user_id`) REFERENCES `auth_user` (`id`))')

解决方式:

在setting文件的databases中添加以下代码取消外键检查

DATABASES = {
	"default": {
		"ENGINE": "django.db.backends.mysql",
		"NAME": "trop",
		"USER": "trop",
		"PASSWORD": "trop",
		"HOST": "127.0.0.1",
		"PORT": "3306",
		"OPTIONS": {
			"init_command": "SET foreign_key_checks = 0;",
		}
	}
}

 

THE END
分享
二维码
打赏
海报
【Python】mysql添加外键报错: 1452 Cannot add or update a child row: a foreign key constraint fails
Python执行程序报错,如下: IntegrityError at /admin/accounts/profile/1/change/ (1452, 'Cannot add or update a child row: a foreign key constraint f……
<<上一篇
下一篇>>