Laravel:使用外键的种子填充器

3

在我的数据库中,我已经有了两个表:通知表和状态表,它们之间存在多对多的关系,因此我创建了一个名为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 Mezenin
抱歉,我的错误......谢谢。 - abraham foto
检查您是否在迁移notifications表之前迁移了alertFrequencies,如果没有,请看下面的答案。 - Gayan
3个回答

1

根据@AlexeyMezenin的回答,基于文档更新: 替换为

$table->integer('notification_id')->unsigned();

带有。
$table->unsignedInteger('notification_id');

0

你需要做的第一件事是添加 unsigned(),因为 id 也是无符号的:

$table->integer('notification_id')->unsigned();

如果它没有帮助,分开键的创建和添加外键约束:
Schema::create('alertFrequencies', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('notification_id')->unsigned();
    $table->timestamps();
});

Schema::table('alertFrequencies', function (Blueprint $table) {
    $table->foreign('notification_id')->references('id')->on('notifications')->onDelete('cascade');
});

@abrahamfoto仍然保留这段代码,因为你的代码无法正常工作。另外,请尝试将alertFrequencies名称更改为alert_frequencies。您的错误提示称Laravel尝试使用小写表名。最后,请确保在notifications表迁移之后运行此迁移。 - Alexey Mezenin

0

在迁移notifications表之前,您需要先迁移alertFrequencies表,因为alertFrequenciesnotification_id引用了notifications表的id

并且,您需要将$table->integer('notification_id');更改为$table->integer('notification_id')->unsigned();,因为您不能接受负数作为外键

还要确保您在`config/database.php'中设置了表引擎为InnoDB

$table->engine = 'InnoDB';

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接