Laravel 5.2 Entrust migrate error, cannot add foreign key constraints
Asked Answered
R

3

8

I installed and configure the Laravel 5.2, its working fine, for User ACL I installed the zizaco/entrust package while running this command php artisan migrate (for creating roles, permissions table etc) getting following error

[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 `` (id) on delete cascade on update cascade)

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

What could be the reason ? am I missing something ? I followed the step wise guideline from entrust site

Reticent answered 12/3, 2016 at 10:56 Comment(0)
R
17

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?

in config/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.

Reticent answered 12/3, 2016 at 11:18 Comment(0)
K
2

I get same error sometimes when I'm trying to add foreign keys in the same migration (same Schema::create block). If I'm creating migration where I'm creating new column:

Schema::create(...
    ...
    $table->integer('categories_id')->unsigned();
    ...
});

And then in the same file I'm setting this column as foreign key:

Schema::table('sub_categories', function (Blueprint $table) {
    $table->foreign('categories_id')->references('id')->on('main_categories');
});

It works perfectly.

Hope this will help you.

Key answered 12/3, 2016 at 11:2 Comment(0)
M
0

If U Use All Other Solution that given in GitHub or StackOverflow and not resolve your issue then delete users table from database admin panel and make some changes in the users migration file. Change the bigIncrements('id'); to Increment('id'). Or Goto Database admin panel like if you use MySQL goto PHPmyadmin panel here goto database and then users here change the datatype of id from bigint(20) into int(10).

Meit answered 17/6, 2020 at 9:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.