1
votes

I am installing the laravel 4 package Entrust. I create the migration file. When i try to run the migration i get an error regarding the foreign key constraint. Is this an issue pretty regarding some PhpMyAdmin settings? I copy under the error directly from powershell:

PS C:\wamp\www\lab\x_pat> php artisan entrust:migration

Tables: roles, assigned_roles, permissions, permission_role An migration that creates 'roles', 'assigned_roles', 'permissions', 'permission_role' tables will be created in app/data base/migrations directory

Proceed with the migration creation? [Yes|no]yes

Creating migration... Migration successfully created!

PS C:\wamp\www\lab\x_pat> php artisan migrate

Migration table created successfully.

[Exception] SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table assigned_roles add const raint assigned_roles_user_id_foreign foreign key (user_id) references users (id)) (Bindings: array ( ))

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

migrate [--bench[="..."]] [--database[="..."]] [--path[="..."]] [--package[="..."]] [--pretend] [--seed]

5

5 Answers

1
votes

Foreign key constraints are not enforced by phpMyAdmin (which is a web interface) but by the MySQL server.

There are many possibilities for this error; it might be because the tables' structure do not permit it (missing indexes for example), or because of the existing data itself that does not respect the constraint you want to add.

1
votes

The error is due to the missing reference on adding the foreign key (user_id) references users (id). Please ensure that the user table is exist prior to running the entrust migration.

1
votes

I had to ensure my users table had its 'id' column attribute set to 'unsigned'. This matches the user_id column attribute in the entrust assigned_roles table. That allowed the migration to proceed for me.

0
votes

SOLVED:

In PhpMyAdmin in had just to go in the table that contains the foreign key and press on INDEX button. In this way an index is created on that foreign key and then in RELATION VIEW is possible to assign the foreign key to the proper id of the proper table.

I think was just that the solution.

0
votes

The root problem is in the file EntrustSetupTables in the migration folder, in this lines :

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

Just put the name of the table users after the .on() and the problem will be solved.