自从mysql升级,以及使用mariaDB以来,很多不曾更新django中model的外键,
今天,按以前的思路写完外键之后,
migrate命令报错:
1005 - Can't create table `xxxDB`.`#sql-1_407` (errno: 150 "Foreign key constraint is incorrectly formed")
很郁闷,跟踪到mysql的日志:
SHOW ENGINE INNODB STATUS;
------------------------
LATEST FOREIGN KEY ERROR
------------------------
2019-06-06 01:52:21 0x7f27202bf700 Error in foreign key constraint of table `xxxDB`.`cmdb_jiraworkflow`:
Alter table `xxxDB`.`cmdb_jiraworkflow` with foreign key constraint failed. Referenced table `xxxDB`.`cmdb_env` not found in the data dictionary near ' FOREIGN KEY (`env_name_id`) REFERENCES `cmdb_env` (`id`)'.
Referenced table not found in the data dictionary near ' FOREIGN KEY REFERENCES .
眼睛绿了,以写django写法有错,改了N次无果。
网上查了N多方案,没有对应我这个的。
最后,想起来了MYSQL升级的事端,以前的存储引擎都是MYISAM,更改为INNODB试试?
https://www.cnblogs.com/abeli/p/6533662.html
https://www.cnblogs.com/Baronboy/p/6037913.html
https://www.cnblogs.com/wanghuaijun/p/5829872.html
https://www.cnblogs.com/afish/p/3969756.html
结果,OK了~~~~
django的o数据库设置也更新如下:
DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'xxxDB', 'USER': 'xx', 'PASSWORD': 'xxx', 'HOST': '10.xx.x.1', 'PORT': '3306', 'OPTIONS': {"init_command": "SET storage_engine=INNODB", } }, }