lundi 29 avril 2019

laravel errno: 150 "Foreign key constraint is incorrectly formed

I user below tutorial for laravel categories :

Laravel categories with dynamic deep paths

I use below code same tutorial for migration :

public function up()
    {
        Schema::create('categories', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('title');
            $table->string('slug');
            $table->integer('parent_id')->unsigned()->default(0);
            $table->timestamps();
        });

        Schema::table('categories', function (Blueprint $table) {
            $table->foreign('parent_id')->references('id')->on('categories')->onUpdate('cascade')->onDelete('cascade');
        });

    }


but I have below error :

SQLSTATE[HY000]: General error: 1005 Can't create table 'xxx'.'#sql-453_147' (errno: 150 "Foreign key constraint is incorrectly formed")

(SQL: alter table 'categories' add constraint 'categories_parent_id_foreign' foreign key ('parent_id') references 'categories' ('id') on delete cascade on update cascade)

Thanks for help



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire