简体   繁体   中英

PHP: Laravel Cannot add foreign key constraint

I have files 2018_08_23_042408_create_roles_table.php

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

class CreateRolesTable extends Migration
{
public function up()
{
    Schema::create('roles', function (Blueprint $table) {
        $table->increments('id');
        $table->string('role_name');
        $table->string('description');
        $table->timestamps();
    });
}
public function down()
{
    Schema::drop('roles');
}
}

and 2018_08_23_042521_create_users_table.php

<?php

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

class CreateUsersTable extends Migration
{
public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('fullname');
        $table->string('email')->unique();
        $table->string('username')->unique();
        $table->string('password');
        $table->string('avatar_link');
        $table->integer('role_id');
        $table->foreign('role_id')->references('id')->on('roles');
        $table->rememberToken();
        $table->timestamps();
    });
}

public function down()
{
    Schema::table('role_user', function (Blueprint $table) {
        $table->dropForeign(['role_id']);
    });
    Schema::drop('users');
}
}

yet when I ran php artisan migrate I got this error

[Illuminate\Database\QueryException]                                         
 SQLSTATE[HY000]: General error: 1215 Cannot add foreign key 
 constraint (SQL  
: alter table `users` add constraint `users_role_id_foreign` foreign 
key (`role_id`) references `roles` (`id`))                                                                                                             

[PDOException]                                                          
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint 

And when I ran php artisan:reset it always shows error like 'Base table exists' and I have to run php artisan tinker and Schema::drop('users') to fix that. I have read similar question on stackoverflow but nothing worked. Any insight of what caused this? Thank you.

You have to use unsignedInteger to role_id, because it is unsinged int in your database (you use increments). And then try to migrate.

$table->unsignedInteger('role_id');

just give unsigned on role_id . change

$table->integer('role_id');

into

$table->integer('role_id')->unsigned();

This is because the foreign key is unsigned integer.

For managing the foreign key relationship two tables must have the same datatype column and the parent table column must be a primary key or an index column . In your case role_id column is an integer and in users table id column is not an integer that's why the error is.

So make these two column identical in terms of datatype and try again.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM