大炮添加外键

Cannon add foreign keys

$table->string('game_id')->unique();
$table->foreign('game_id')->references('game_id')->on('servers');

在我的游戏迁移中,我得到了以下信息:

 SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL

: alter table games add constraint games_game_id_foreign foreign key ( game_id) references servers (game_id))

以及我的服务器迁移:

$table->string('game_id');
$table->foreign('game_id')->references('game_id')->on('games');

如何解决这个问题? 注意:服务器 table 上的 game_id 不应该是唯一的,因为它们可以包含具有相同类型游戏的多个服务器。 我在游戏路线上使用关系来显示基于相同类型的服务器。

您使用 servers table 的 id 定义外键 game_id,因此将 game_id 更改为 id 作为参考,例如:

$table->unsignedBigInteger('game_id');
$table->foreign('game_id')->references('id')->on('servers');