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

今天在回顾django模型,创建一对多对象时,报错:

django.db.utils.IntegrityError: (1452, 'Cannot add or update a child row: a foreign key constraint fails (`djangosite`.`contact
1`, CONSTRAINT `contact1_account_id_a6fda630_fk_app_account_id` FOREIGN KEY (`account_id`) REFERENCES `app_account` (`id`))')

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

'OPTIONS':{ "init_command":"SET foreign_key_checks = 0;",

具体放到这里:

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

 

你可能感兴趣的:(1452, Cannot add or update a child row: a foreign key constraint fails)