Answer the question
In order to leave comments, you need to log in
General error: 1215 Cannot add foreign key constraint Laravel 5.3?
I don't understand why, the first foreign_key works, but the second one doesn't.
// это pivot table, беру id юзера, делаю связь, все работает, но не надет поставить связь с roles таблицей
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');
});
[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
Answer the question
In order to leave comments, you need to log in
Since Laravel 5.8, the autoincrement method (id) has changed. was
And now:
Therefore, bind to bigInteger
$table->bigInteger('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question