entrust
entrust copied to clipboard
Error running new migration
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.
The problem is already covered in the documentation, but your missing a unsigned();
I have the same issue. And it is the same for users and role_user.
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.
Thank you very much EmadAdly ...
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(); });