- Sudipta Das
- Posts : 14
Join date : 2020-07-25
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.
Code:-
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');
});
}
- Yourstanmay
- Posts : 2
Join date : 2020-07-25
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.
for bigincrements expected data type is bigInteger('column_name')->unsigned();
for increments expected is integer('column_name')->unsigned(); etc.
- Arghya De
- Posts : 6
Join date : 2020-07-25
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.
- Sudipta Das
- Posts : 14
Join date : 2020-07-25
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
Permissions in this forum:
You cannot reply to topics in this forum
|
|