lundi 10 mai 2021

Laravel/MySQL: errno 150 "Foreign key constraint is incorrectly formed")

I'm on Laravel 5.4, PHP 5.6, Ubuntu 18.04. When I run php artisan migrate, I get:

In Connection.php line 647:
                                                                                                                     
  SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'post_id' (SQL: alter table `comments` add `po  
  st_id` int unsigned not null, add `user_id` int unsigned null)                                                     
                                                                                                                     

In Connection.php line 449:
                                                                                
  SQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'post_id' 

I'm trying to use https://github.com/klisl/laravel-comments. Before trying to perform a migration with this package I had created DB at phpMyAdmin, had configured .env by adding DB name and stuff, had successfully run php artisan migrate, php artisan make:auth and php artisan make:controller AuthController. Then, after running php artisan vendor:publish --provider="Klisl\Comments\CommentsServiceProvider" I get 2 new files in migrations folder: date_number_CreateCommentsTable.php and date_number_ChangeCommentsTable.php

Here's source from these 2 files:

CreateCommentsTable.php:

<?php

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

/**
 * Class CreateCommentsTable
 */
class CreateCommentsTable extends Migration
{

    /** @return void */
    public function up()
    {
        Schema::create('comments', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email');
            $table->text('text');
            $table->integer('parent_id')->nullable(); //разрешаем null; 
            $table->boolean('status')->default(config('comments.show_immediately')); 
            $table->timestamps();
        });
    }

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

}

ChangeCommentsTable.php:

<?php

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

/**
 * Class ChangeCommentsTable
 */
class ChangeCommentsTable extends Migration
{

    /** @return void */
    public function up()
    {
        Schema::table('comments', function (Blueprint $table) { 
            $table->integer(config('comments.key_field'))->unsigned();      
            $table->foreign(config('comments.key_field'))->references('id')->on(config('comments.key_table'));  
            
            if(config('comments.user')){
                $table->integer('user_id')->unsigned()->nullable(); //разрешаем null        
                $table->foreign('user_id')->references('id')->on('users');
            }
        });
    }

    /** @return void */
    public function down()
    {
        Schema::table('comments', function (Blueprint $table) {
            //
        });
    }

}

So then I run php artisan migrate and get the error I've written about above.

I've already tried adding ->unsigned() at CreateCommentsTable. Also I've tried to put the foreign keys out of the function at ChangeCommentsTable like this:

    /** @return void */
    public function up()
    {
        Schema::table('comments', function (Blueprint $table) { 
            $table->integer(config('comments.key_field'))->unsigned();          
            
            if(config('comments.user')){
                $table->integer('user_id')->unsigned()->nullable(); //разрешаем null        
            }
        });

        Schema::table('comments', function ($table){
           $table->foreign(config('comments.key_field'))->references('id')->on(config('comments.key_table'));
        });
        Schema::table('comments', function ($table){
            if(config('comments.user')){
                $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            }
        });
    }

    /** @return void */
    public function down()
    {
        Schema::dropForeign([config('comments.key_field')]);
        Schema::dropForeign(['user_id']);
        Schema::table('comments', function (Blueprint $table) {
            //
        });
    }

and this:

Schema::table('comments', function ($table){
   $table->foreign(config('comments.key_field'))->references('id')->on(config('comments.key_table'));
    if(config('comments.user')){
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
    }
});

As any of didn't work out, I decided to post the default version of source above. If you help me with this, you really save my day c:



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire