S
S
StrangeGoogle2020-01-27 16:18:57
Laravel
StrangeGoogle, 2020-01-27 16:18:57

How to solve the migration error?

I'm trying to implement a link of tables, but the following error occurs when trying to make a migration.

Mistake:

<i>Illuminate\Database\QueryException  : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `permission_role` add constraint `permission_role_permission_id_foreign` foreign key (`permission_id`) references `permissions` (`id`) on delete cascade)</i>


Table `users`
Schema::create('users', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->unsignedInteger('role_id')->index();
            $table->foreign('role_id')->references('id')->on('roles');
            $table->rememberToken();
            $table->timestamps();
        });


Table `roles`

Schema::create('roles', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('description')->nullable();
            $table->timestamps();
        });


`permissions` table
Schema::create('permissions', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name')->nullable();
            $table->string('key')->nullable();
            $table->string('controller');
            $table->string('method');
            $table->timestamps();
        });

        Schema::create('permission_role', function (Blueprint $table) {
            $table->unsignedInteger('permission_id');
            $table->unsignedInteger('role_id');
            $table->foreign('permission_id')
                ->references('id')
                ->on('permissions')
                ->onDelete('cascade');
            $table->foreign('role_id')
                ->references('id')
                ->on('roles')
                ->onDelete('cascade');
            $table->primary(['permission_id', 'role_id']);
        });


Hierarchy...

5e2ee33d1ae11907830680.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
adam_carraway, 2020-01-27
@StrangeGoogle

How to fix General error: 1215 Cannot add foreign key constraint?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question