django admin报错IntegrityError:'Cannot add or update a child row: a foreign key constraint fails'

如果在使用Django Admin后台添加用户时出现报错

(1452, 'Cannot add or update a child row: a foreign key constraint fails (`mxproject`.`django_admin_log`, CONSTRAINT `django_admin_log_user_id_c564eba6_fk_auth_user_id` FOREIGN KEY (`user_id`) REFERENCES `auth_user` (`id`))')

这时候需要在setting文件的databases中添加以下代码取消外键检查

'OPTIONS':{
        "init_command":"SET foreign_key_checks = 0;",
完整的就是:
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'mxproject',
        'USER':'root',
        'PASSWORD':'123456',
        'HOST':'127.0.0.1',
        'PORT':'3306',
        'OPTIONS':{
        "init_command":"SET foreign_key_checks = 0;",
    }
 
    }
  1. 转发自https://blog.csdn.net/w371500241/article/details/78269566

你可能感兴趣的:(Django)