Voyager 安装时外键约束的格式不正确 Laravel

Foreign key constraint is incorrectly formated on Voyager Installation Laravel

我正在尝试在我的 laravel 应用程序上安装航海者,当我 运行 在我的控制台 "php artisan voyager:install --with-dummy" 中时,我收到此错误

PS C:\xampp\htdocs\newlootodds> php artisan voyager:install --with-dummy
Publishing the Voyager assets, database, and config files
Copied Directory [\vendor\tcg\voyager\publishable\database\seeds] To [\database\seeds]
Publishing complete.
Publishing complete.
Migrating the database tables into your application
Migrating: 2016_11_30_141208_create_permission_role_table

   Illuminate\Database\QueryException  : SQLSTATE[HY000]: General error: 1005 Can't create table `laravelapp`.`permissio
n_role` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `permission_role` add constraint `
permission_role_permission_id_foreign` foreign key (`permission_id`) references `permissions` (`id`) on delete cascade)

  at C:\xampp\htdocs\newlootodds\vendor\laravel\framework\src\Illuminate\Database\Connection.php:664
    660|         // If an exception occurs when attempting to run a query, we'll format the error
    661|         // message to include the bindings with SQL, which will make this exception a
    662|         // lot more helpful to the developer instead of just the database's errors.
    663|         catch (Exception $e) {
  > 664|             throw new QueryException(
    665|                 $query, $this->prepareBindings($bindings), $e
    666|             );
    667|         }
    668|

  Exception trace:

  1   Doctrine\DBAL\Driver\PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table `laravelapp`.`permiss
ion_role` (errno: 150 "Foreign key constraint is incorrectly formed")")
      C:\xampp\htdocs\newlootodds\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOStatement.php:119

  2   PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table `laravelapp`.`permission_role` (errno: 150
 "Foreign key constraint is incorrectly formed")")
      C:\xampp\htdocs\newlootodds\vendor\doctrine\dbal\lib\Doctrine\DBAL\Driver\PDOStatement.php:117

我花了两天时间在 google 上寻找解决方案,但仍然没有找到任何东西..

检查您的“2016_11_30_141208_create_permission_role_table”迁移。据我了解,您有一个支点 table。我认为你有一些问题,像这样

public function up()
{
    Schema::create('permission_role', function(Blueprint $table)
    {
        $table->bigIncrements('id');

        $table->bigInteger('permission_id')->unsigned();
        $table->bigInteger('role_id')->unsigned();

        $table->foreign('permission_id')->references('id')->on('permissions')->onDelete('cascade');
        $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
    });
}

由于 bigIncrements() 创建一个无符号整数列,您也需要将外键列定义为无符号整数。因此,请确保您更改为:

public function up()
{
    Schema::create('permission_role', function(Blueprint $table)
    {
        $table->bigIncrements('id');

        $table->unsignedBigInteger('permission_id')->unsigned();
        $table->unsignedBigInteger('role_id')->unsigned();

        $table->foreign('permission_id')->references('id')->on('permissions')->onDelete('cascade');
        $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
    });
}

顺便说一下,如果这没有帮助,那么我至少需要查看您的 3 个迁移(权限、角色、permission_role)