【发布时间】:2022-06-15 22:37:59
【问题描述】:
我想创建一个表,其中有两个条目引用我的“用户”表中的不同用户。我使用 user_id 和 from_id。
运行迁移时,我收到错误消息“外键约束格式不正确”。 当我删除两个 from_id 行时,它可以工作。 这是我的迁移:
public function up()
{
Schema::create('applicationpicture', function (Blueprint $table) {
$table->id();
$table->char('url')->default('');
// When I remove the following two lines, everything works.
$table->foreignId('from_id');
$table->foreign('from_id')->references('id')->on('users')->onDelete('set null');
$table->foreignId('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreignId('event_id')->nullable();
$table->foreign('event_id')->references('id')->on('events')->onDelete('set null');
$table->timestamps();
});
}
【问题讨论】: