entrust icon indicating copy to clipboard operation
entrust copied to clipboard

Error running new migration

Open codecomp opened this issue 9 years ago • 5 comments

I followed the instructions on a fresh project on a fresh homebrew install for laravel 5. And I've encountered the following error when running php artisan migrate:

  [Illuminate\Database\QueryException]
  SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL
  : alter table `role_user` add constraint role_user_user_id_foreign foreign
  key (`user_id`) references `users` (`id`) on delete cascade on update casca
  de)

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

I also the same problem when on my work machine I tried running php artisan entrust:migration and php artisan migrate on an existing Laravel 5 project.

codecomp avatar May 26 '15 14:05 codecomp

The problem is already covered in the documentation, but your missing a unsigned();

avlastenok avatar Jun 17 '15 18:06 avlastenok

I have the same issue. And it is the same for users and role_user.

12-12-2015 23_34_09 12-12-2015 23_33_58 12-12-2015 23_33_41 12-12-2015 23_33_31

dsbilling avatar Dec 12 '15 22:12 dsbilling

I fixed the issue, in entrust migration file, there was users table name missing. see following line

$table->foreign('user_id')->references('id')->on(' ')->onUpdate('cascade')->onDelete('cascade');

So I changed to this,

$table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade')->onDelete('cascade'); I added the users table name, and issue is fixed.

Reason, Why I got this issue?

inconfig/auth.php file, there was not a 'table'=>'users' key/pair mentioned in providers array, see below (this is default, means when fresh laravel is installed)

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
    ],

while php artisan entrust:migration command runs, it pulls the users table name from above providers array, if there is no table mentioned then in migration file, relationship sets empty like this.

$table->foreign('user_id')->references('id')->on('')->onUpdate('cascade')->onDelete('cascade'); So, add table in provider array like this.

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
        'table'=>'users'
    ],

after that run command for entrust migration php artisan entrust:migration this will generate the proper migration file.

EmadAdly avatar Mar 24 '16 18:03 EmadAdly

Thank you very much EmadAdly ...

karim-khan avatar Aug 02 '16 06:08 karim-khan

In laravel 5.8.10

add 'table'=>'users' in config/auth.php

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\User::class,
        'table'=>'users'
    ],

and replace users schema bigIncrements to increments

Schema::create('users', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name');
            $table->string('email')->unique();
            $table->timestamp('email_verified_at')->nullable();
            $table->string('password');
            $table->rememberToken();
            $table->timestamps();
        });

pubsajib avatar Apr 07 '19 21:04 pubsajib