abraham foto
abraham foto

Reputation: 457

laravel : seeder with foriegn keys

in my database i have already tables namely: notifications table, statuses table, they have many to many relation, that is why i have a pivot table called notification_status. i created them with migrations and seed them with a seeder, all works fine. now i realize that i need one extra table which has many to one relation with notifications table(natification->hasMany->alertfrequency). when i tried to migrate it, it did allow me to do so. here is my notification table

<?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');
    }
}

and the alert frequency table, the new table i want to add,

 <?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');
        }
    }

when i tried to add i get the following constrain

  [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)

any one with a idea or suggestion. i appreciate all idea and suggestions.

Upvotes: 3

Views: 120

Answers (3)

Gayan
Gayan

Reputation: 3704

You need to migrate notifications table prior to alertFrequencies table as notification_id of alertFrequencies is referencing id of notifications table.

And you need to change $table->integer('notification_id'); to $table->integer('notification_id')->unsigned(); as you cannot accept minus ids as foreign key

And also make sure you have set your table engine set to InnoDB in your `config/database.php'.

$table->engine = 'InnoDB';

Upvotes: 0

AddWeb Solution Pvt Ltd
AddWeb Solution Pvt Ltd

Reputation: 21691

From @AlexeyMezenin's answer, update based on Documentation: Replace

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

with

$table->unsignedInteger('notification_id');

Upvotes: 1

Alexey Mezenin
Alexey Mezenin

Reputation: 163978

First thing you should do is to add unsigned(), because id is unsigned too:

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

If it'll not help, divide key creation and adding a foreign key constraint:

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');
});

Upvotes: 0

Related Questions