Paul Sorrells
Paul Sorrells

Reputation: 57

Foreign key constraint is incorrectly formated on Voyager Installation Laravel

I am trying to install voyager on my laravel app and when I run in my console "php artisan voyager:install --with-dummy" I get this error

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

I have two days trying to find the solution on google and still haven't found anything..

Upvotes: 1

Views: 997

Answers (1)

boolfalse
boolfalse

Reputation: 2101

Check your "2016_11_30_141208_create_permission_role_table" migration. As I understand you have a pivot table. And I think there you have some issue, something like this

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');
    });
}

Since bigIncrements() creates an unsigned integer column, you need to define the foreign key column as unsigned integer too. So make sure that you changed to this:

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');
    });
}

By the way, If it will not help, then I need to see at least your 3 migrations (permissions, roles, permission_role)

Upvotes: 2

Related Questions