Binaryfolks 2020 Dev Assignment
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Go down
avatar
Sudipta Das
Posts : 14
Join date : 2020-07-25

errno: 150 "Foreign key constraint is incorrectly formed" Empty errno: 150 "Foreign key constraint is incorrectly formed"

Fri Jul 31, 2020 9:49 pm
Getting this error while adding foreign key in my table.
errno: 150 "Foreign key constraint is incorrectly formed" Screen11

Code:-
Code:
public function up()
    {
        Schema::create('articles', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedBigInteger('user_id');
            $table->string('title');
            $table->text('excerpt');
            $table->text('body');
            $table->timestamps();

            $table->foreign('user_id')
                ->references('id')
                ->on('users')
                ->onDelete('cascade');
        });
    }
avatar
Yourstanmay
Posts : 2
Join date : 2020-07-25

errno: 150 "Foreign key constraint is incorrectly formed" Empty Re: errno: 150 "Foreign key constraint is incorrectly formed"

Fri Jul 31, 2020 10:07 pm
the referenced and referencing fields must have exactly the same data type. this can not be stressed enough.

for bigincrements expected data type is bigInteger('column_name')->unsigned();

for increments expected is integer('column_name')->unsigned(); etc.
avatar
Arghya De
Posts : 6
Join date : 2020-07-25

errno: 150 "Foreign key constraint is incorrectly formed" Empty Re: errno: 150 "Foreign key constraint is incorrectly formed"

Fri Jul 31, 2020 10:41 pm
Code:
public function up()
    {
        Schema::create('articles', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('user_id');
            $table->string('title', 100);
            $table->text('excerpt');
            $table->text('body');
            $table->timestamps();

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

Try This.
avatar
Sudipta Das
Posts : 14
Join date : 2020-07-25

errno: 150 "Foreign key constraint is incorrectly formed" Empty Re: errno: 150 "Foreign key constraint is incorrectly formed"

Fri Jul 31, 2020 11:37 pm
It's ok I've solved it already. Actually there wasn't any fault in code, the fault was somehow I created the user table after the article table, but it should be: user table first and then the other tables. Now its working fine. Thanks for the responses tho.<3
Sponsored content

errno: 150 "Foreign key constraint is incorrectly formed" Empty Re: errno: 150 "Foreign key constraint is incorrectly formed"

Back to top
Permissions in this forum:
You cannot reply to topics in this forum