Answer the question
In order to leave comments, you need to log in
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>
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();
});
Schema::create('roles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('description')->nullable();
$table->timestamps();
});
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']);
});
Answer the question
In order to leave comments, you need to log in
How to fix General error: 1215 Cannot add foreign key constraint?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question