在pycharm环境下连接MySQL数据库
执行命令 python manage.py migrate
控制台报错:
(1452, 'Cannot add or update a child row: a foreign key constraint fails (`django03`.`TwoApp_idcard`, CONSTRAINT `TwoApp_idcard_i_person_id_131068ba_fk_TwoApp_person_id` FOREIGN KEY (`i_person_id`) REFERENCES `TwoApp_person` (`id`))')
解决方案:
在setting文件的databases中添加以下代码取消外键检查
'OPTIONS':{"init_command":"SET foreign_key_checks = 0;"},
完整代码如下:
完整的就是:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'databaseName',
'USER':'root',
'PASSWORD':'123456',
'HOST':'127.0.0.1',
'PORT':'3306',
'OPTIONS':{"init_command":"SET foreign_key_checks = 0;"},
}
}