A
A
adam_carraway2020-01-03 21:55:35
Laravel
adam_carraway, 2020-01-03 21:55:35

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

I'm trying to assign a foreign key, but when executing migrate, I get this error, I don't 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');
        });
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
k2lhu, 2020-01-03
@adam_carraway

See what the id column in the users table looks like in the database - bigint(20), unsigned In the socialites
table , before hanging up the foreign key, put the user_id in order

$table->unsignedBigInteger('user_id')->nullable(false);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question