General error: 1215 Cannot add foreign key constraint Laravel 5.3?

I do not understand why the first foreign_key works and the other doesn't.

// this is a pivot table, take the id of the user, make the connection, it works, but not wearing to put the relationship with the roles table
 Schema::create('user_role', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->integer('user_id')->unsigned()->nullable();
$table->integer('role_id')->unsigned()->nullable();
$table->timestamps();
$table->foreign('user_id')->references('user_id')->on('users')->onDelete('cascade');
 // $table->foreign('id')->references('role_id')->on('roles')->onDelete('cascade');
 });


This error:
[Illuminate\Database\QueryException]
 SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter
 table `user_role` add constraint `user_role_id_foreign` foreign key (`id`) referenc
 es `roles` (`role_id`) on delete cascade)



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

Use InnoDB.


That I overlooked?
August 23rd 19 at 10:53
4 answers
August 23rd 19 at 10:59
This error happens when the field is referenced by a key has a different type, for example there is unsigned.
//the roles table 
 Schema::create('roles', function (Blueprint $table) {
$table->increments('id')->unsigned();
 $table->string('name', 55)->unique();
 $table->string('description', 255)->nullable();
$table->timestamps();
 });
- Adalberto.Blick commented on August 23rd 19 at 11:02
August 23rd 19 at 11:01
I hope I'm wrong, but as far as I ponmnyu you cannot call $table->foreign() before calling Schema::create.
I have earned a snippet:

if (!Schema::hasTable('d_handlers')) {
 Schema::create('d_handlers', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->integer('module')->unsigned();
$table->timestamps();
});
 Schema::table('d_handlers', function($table) {
$table->foreign('module')->references('id')->on('d_modules')->onDelete('cascade');
});
}
why, then, fulfills the binding first? The second binding does not plow. It is possible to sequence the tables are created, trying to bind it to a table which is not created yet - Adalberto.Blick commented on August 23rd 19 at 11:04
August 23rd 19 at 10:55
Solution
Solved the problem. Thank you Laravel and its magic.

The staging table you must create after the basic users and roles, only after them, to create an intermediate user_role!

The problem was in the order of creation of tables ( migrations ) - remove the migration, and create a new one.
August 23rd 19 at 10:57
This:
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
foreign('user_id') is a foreign key which is connected, your version is not working. - Adalberto.Blick commented on August 23rd 19 at 11:00
Besides, as I wrote above, the users table works as well with roles - no - Adalberto.Blick commented on August 23rd 19 at 11:03
: https://laravel.com/docs/5.3/migrations#foreign-ke... So the docks do not write) To references you have to specify the field name from the table relations, and field name (id) of the base table (users). You have two lines it is written (for role_id and user_id). Thought that one example is enough (for user_id). Should be and role_id $table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade'); - jedidiah13 commented on August 23rd 19 at 11:06

Find more questions by tags LaravelPHPSQL