在我的数据库中,我已经有了两个表:通知表和状态表,它们之间存在多对多的关系,因此我创建了一个名为notification_status的透视表。我使用迁移创建它们,并使用种子填充数据,一切都很正常。现在我意识到我需要一个额外的表,它与通知表存在多对一的关系(通知->hasMany->alertfrequency)。但是当我尝试进行迁移时,它不允许我这样做。 以下是我的通知表:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateNotificationsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('notifications', function (Blueprint $table) {
$table->increments('id');
$table->string('website_url');
$table->string('email');
$table->string('slack_channel');
$table->string('check_frequency');
$table->string('alert_frequency');
$table->string('speed_frequency');
$table->boolean('active');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('notifications');
}
}
还有警报频率表,我想要添加的新表格。
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateAlertFrequenciesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('alertFrequencies', function (Blueprint $table) {
$table->increments('id');
$table->integer('notification_id');
$table->foreign('notification_id')
->references('id')->on('notifications')
->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('alertFrequencies');
}
}
当我尝试添加时,出现了以下约束。
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `alertFrequencies` add constraint `alertfrequencies_notification_id_foreign` foreign key (`notification_
id`) references `notifications` (`id`) on delete cascade)
任何有想法或建议的人。我欣赏所有的想法和建议。
alertFrequencies
表的迁移文件。 - Alexey Mezeninnotifications
表之前迁移了alertFrequencies
,如果没有,请看下面的答案。 - Gayan