错误代码:3175。无法在基列具有外部约束的虚拟列上创建索引

Error Code: 3175. Cannot create index on virtual column whose base column has foreign constraint

在 MySQL 5.7.11 中,添加其中一列是虚拟生成列的复合索引会导致以下错误:

Error Code: 3175. Cannot create index on virtual column whose base column has foreign constraint

实际上,生成虚拟列的基列没有任何外部约束,所以错误信息是莫名其妙的。

在MySQL5.7.15.

中没有出现错误

报错信息有误。实际问题是索引中的其他两个列在其上定义了外键,这些外键设置为 on update cascade。在 MySQL 5.7.14:

之前不允许这样做

In MySQL 5.7.13 and earlier, InnoDB does not permit defining cascading referential actions on non-virtual foreign key columns that are explicitly included in a virtual index. This restriction is lifted in MySQL 5.7.14. (source)

将键更改为 on update restrict 可使错误消失。这也解释了为什么在MySQL 5.7.15.

中不会出现这个错误