1452, 'Cannot add or update a child row: a foreign key constraint fails

(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中添加以下代码取消外键检查

 

 

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'zhq2',
        'USER': 'root',
        'PASSWORD': '211314',
        'HOST': '127.0.0.1',
        'PORT': 3306,
        # 取消外键的检查(admin后台添加数据会报错)
        'OPTIONS': {
            "init_command": "SET foreign_key_checks = 0;",
        }
    }
}

 

  

 

posted @ 2019-08-01 17:44  堪舆先森  阅读(1265)  评论(0编辑  收藏  举报