laravel:带有外键的播种机

时间:2017-01-13 10:56:19

标签: php laravel

在我的数据库中我已经有了表:通知表,状态表,它们有多对多的关系,这就是为什么我有一个名为notification_status的数据透视表。我用迁移创建它们并用播种机播种它们,一切正常。现在我意识到我需要一个额外的表,它与通知表有多对一的关系(natification-> 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)

任何有想法或建议的人。我很感激所有的想法和建议。

3 个答案:

答案 0 :(得分:1)

来自@ AlexeyMezenin的答案,根据Documentation进行更新: 取代

y <- cbind(x,z)
colnames(y) <- make.names(colnames(y), unique = TRUE)

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

答案 1 :(得分: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');
});

答案 2 :(得分:0)

您需要在notifications表之前迁移alertFrequencies表,notification_id的{​​{1}}引用alertFrequencies表的id

您需要将notifications更改为$table->integer('notification_id');,因为您无法接受$table->integer('notification_id')->unsigned(); ID minus

并确保在`config / database.php&#39;中将表引擎设置为foreign key

InnoDB