How to fix General error: 1215 Cannot add foreign key constraint?

Trying to assign foreign key but when you run migrate, I get this this error, I do not understand what the problem is.
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `socialites` add constraint `socialites_user_id_foreign` foreig
n key (`user_id`) references `users` (`id`) on delete CASCADE)

General error: 1215 Cannot add foreign key constraint

public function up()
{
 Schema::create('socialites', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('user_id')->index();
$table->string('provider_user_id');
$table->string('provider');
$table->timestamps();

});
 Schema::table('socialites', function (Blueprint $table) {

$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('CASCADE');
});
 }
April 4th 20 at 00:41
2 answers
April 4th 20 at 00:43
Solution
The type of fields must be the same. In your case it is
// $table->integer('user_id')->index();
$table->unsignedBigInteger('user_id')->index();
April 4th 20 at 00:45
Solution
Let's see what our database consists of column id in table users - bigint(20), unsigned
The table socialites before you hang the foreign key, tidy user_id
$table->unsignedBigInteger('user_id')->nullable(false);
Thanks, I forgot about that) - Ruper commented on April 4th 20 at 00:48

Find more questions by tags Laravel