jcadima
3/1/2017 - 4:16 AM

Laravel : Cannot add foreign key constraint

Laravel : Cannot add foreign key constraint


http://stackoverflow.com/questions/22615926/migration-cannot-add-foreign-key-constraint-in-laravel

Go to the 'migrations' table and delete the entries of these 2 migrations is they are listed
or you will always get the error about the foreign key, even if the tables are deleted

php artisan migrate:status

you should see the tables that migrated, if you see a 'Y' in one of them , it needs
to be deleted in the 'migrations' table like its described above

Also change the timestamp to be before the table that needs to reference the foreign key
for example  the 'CreateCommentsTable' calls the foreign key of 'posts', but we know that 'posts'
has a timestamp before this table is run , so this is fine

Now  the CreateCommentsRepliesTable calls the foreign key of 'comments', so this needs a timestamp 
after the 'CreateCommentsTable' because 'CreateCommentsTable' creates the 'comments' table first, 
it cant have a timestamp before that.

===================== CreateCommentsTable ==================================

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCommentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('comments', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('post_id')->unsigned() ; // Needs to be Unsigned
            $table->integer('is_active')->default(0) ;
            $table->string('author');
            $table->string('email') ;
            $table->text('body');
            $table->timestamps();

            // Cascade on Delete
            $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade') ;
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('comments');
    }
}

?>

============ CreateCommentsRepliesTable =====================

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCommentRepliesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('comment_replies', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('comment_id')->unsigned(); // Needs to be Unsigned
            $table->integer('is_active')->default(0) ;
            $table->string('author');
            $table->string('email') ;
            $table->text('body');
            $table->timestamps();
 
            // Cascade on Delete
            $table->foreign('comment_id')->references('id')->on('comments')->onDelete('cascade') ;

        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('comment_replies');
    }
}