PHP:Laravel无法添加外键约束

PHP:Laravel无法添加外键约束,php,mysql,laravel,laravel-5,Php,Mysql,Laravel,Laravel 5,我有文件2018\u 08\u 23\u 042408\u create\u roles\u table.php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateRolesTable extends Migration { public function up() {

我有文件2018\u 08\u 23\u 042408\u create\u roles\u 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');
}
}
和2018_08_23_042521_create_users_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');
}
}
<?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');
}
}
当我运行php artisan:reset时,它总是显示类似“基表存在”的错误,我必须运行php artisan tinker和Schema::drop(“用户”)来修复这一错误。
我读过关于stackoverflow的类似问题,但没有任何效果。你知道这是什么原因吗?谢谢。

您必须使用unsignedInteger到role\u id,因为它在您的数据库中是非单整数(您使用增量)。然后尝试迁移

$table->unsignedInteger('role_id');

您必须使用unsignedInteger来定义角色\u id,因为它在数据库中是非单整数(您使用增量)。然后尝试迁移

$table->unsignedInteger('role_id');

为了管理
外键
关系,两个表必须具有相同的数据类型列,并且父表列必须是
主键
索引列
。在您的情况下,
role\u id
列是一个整数,而
users
id
列不是一个整数,这就是错误的原因


因此,请使这两列在数据类型方面相同,然后重试。

要管理
外键关系,两个表必须具有相同的数据类型列,并且父表列必须是
主键
索引列
。在您的情况下,
role\u id
列是一个整数,而
users
id
列不是一个整数,这就是错误的原因


因此,请使这两列在
数据类型方面相同,然后重试。

只需在
角色id上给出
未签名的
。
改变

进入


这是因为外键是无符号整数。

只需在
role\u id
上给出
unsigned
。 改变

进入


这是因为外键是无符号整数。

对外键列使用无符号类型:
$table->unsignedInteger('role_id')外键列使用未签名类型:
$table->unsignedInteger('role_id')谢谢,已解决。我错过了user表中的unsigned()。谢谢,已解决。我错过了user表中的unsigned()。
$table->integer('role_id')->unsigned();