[英]Laravel - Foreign key constraint is incorrectly formed - missing nullable
I want to create a table with two entries referencing different users in my 'users' table.我想创建一个表,其中有两个条目引用我的“用户”表中的不同用户。 I use user_id and from_id.我使用 user_id 和 from_id。
When I run the migration, I get an error 'Foreign key constraint is incorrectly formed'.运行迁移时,出现错误“外键约束格式不正确”。 When I remove the two from_id lines it works.当我删除两个 from_id 行时,它可以工作。 Here is my migration:这是我的迁移:
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();
});
}
Do it this way, there's more simple and clean way to do the same from Laravel 7.x:这样做,Laravel 7.x 有更简单和干净的方法来做同样的事情:
public function up()
{
Schema::create('applicationpicture', function (Blueprint $table) {
$table->id();
$table->char('url')->default('');
$table->foreignId('from_id')->nullable()->constrained('users');
$table->foreignId('user_id')->nullable()->constrained()->cascadeOnDelete();
$table->foreignId('event_id')->nullable()->constrained();
$table->timestamps();
});
}
The reason your migrations fail is your foreign keys are set to null onDelete but are not nullable at same time.迁移失败的原因是您的外键设置为 null onDelete 但不能同时为空。
public function up()
{
Schema::create('applicationpicture', function (Blueprint $table) {
$table->id();
$table->char('url')->default('');
// Problem lies here just add nullable()
$table->foreignId('from_id')->nullable();
$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();
});
}
The simple reason is that I use onDelete('set null') for the from_id field, but I forgot to add the nullable() definition.原因很简单,我对 from_id 字段使用了 onDelete('set null'),但我忘记添加 nullable() 定义。 So the correct Migration looks like this:所以正确的迁移看起来像这样:
public function up()
{
Schema::create('applicationpicture', function (Blueprint $table) {
$table->id();
$table->char('url')->default('');
$table->foreignId('from_id')->nullable(); // Added nullable()
$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();
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.