MySQL碰到ErrorCode1215,Cannot add foreign key constraint
今天使用prisma deploy的时候碰到了问题。一直过不去。
这是一个带有关系的配置。在配置文件中的表现是:
type xxxx1 { relation:[xxxx2!]! field1:String field2:String field3:String field4:String ... }
为了加快命令执行速度,我已经把数据库里的数据都清空了(上线时是把正式表rename,然后创建一个空的正式表,上线结束后再恢复数据)。
一般1-2s就会结束。但是今天的命令运行一直不出结果。
测试环境的prisma 是安装在docker 中的。 可以把日志打印出来看看咋回事。
打印日志的命令 ,对于错误日志也要打印到log中。 b64值得是docker ps获取到的ID
# docker logs b64 >/tmp/docker20200722.log 2>&1
通过查看日志,发现有如下的警告:
Encountered exception while applying migration. Rolling back. java.sql.SQLException: (conn=2355294) Cannot add foreign key constraint java.sql.SQLException: (conn=2355294) Cannot add foreign key constraint ... Caused by: java.sql.SQLException: Cannot add foreign key constraint Query is: CREATE TABLE _xxxx2toxxxx1 ...
Cannot add foreign key constraint
通过搜索,发现如果报这个错误,说明两个表的字符集不一致。修改成相同的字符集则可以顺利通过了。
其他情况也可能导致报此错误,比如存储引擎是 innodb 才支持外键。