Foreign Key error: MySQL ERROR 1005 (HY000): Can't create table

Foreign Key error: MySQL ERROR 1005 (HY000): Can't create table

我遇到问题,收到错误消息:

MySQL ERROR 1005 (HY000): Can't create table

每当我尝试为 link 我的两个 table 创建一个外键时。我尝试了多种不同的代码措辞方式,但 none 似乎修复了错误,我假设的当前代码最接近工作:

ALTER TABLE requests
    ADD FOREIGN KEY FK_UserRequest(device_id) REFERENCES users(device_id)

我通读了许多关于这个主题的其他帖子,但似乎没有找到关于为什么这不起作用的简明答案。我希望有人能具体回答这些问题:

背景信息-

是的,您需要确保引用的键是某种索引。

取自:https://dev.mysql.com/doc/refman/5.6/en/create-table-foreign-keys.html

MySQL requires indexes on foreign keys and referenced keys [...]

InnoDB permits a foreign key to reference any column or group of columns. However, in the referenced table, there must be an index where the referenced columns are listed as the first columns in the same order.