Laravel migration fails with SQLSTATE[HY000]: General error: 1215 - laravel-5

I'm trying to add a migration with laravel. This is mi migration file.
public function up() {
Schema::create('sl_categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title')->nullable();
$table->integer('user_id')->unsigned()->nullable();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->softDeletes();
$table->timestamps();
});
Schema::create('sl_images', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('image')->nullable();
$table->integer('sl_category_id')->unsigned()->nullable();
$table->integer('display_order')->nullable();
$table->softDeletes();
$table->timestamps();
});
Schema::table('sl_images', function(Blueprint $table) {
$table->foreign('sl_category_id')->references('id')->on('sl_categories')->onDelete('cascade');
});
}
public function down() {
Schema::dropIfExists('sl_images');
Schema::dropIfExists('sl_categories');
}
But unfortunately I'm getting this error.
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error:
1215 Cannot add foreign key constraint (SQL: alter table sl_images
add constraint sl_images_sl_category_id_foreign foreign key
(sl_category_id) references sl_categories (id) on delete
cascade)

Finally I found the answer. The issue is InnoDB does not permit the creation of a foreign key constraint where a column references a nonmatching column type. If simply said,
This is a big integer
$table->bigIncrements('id');
and this is a simple integer.,
$table->integer('sl_category_id')->unsigned()->nullable();
So we have to change the later line to bigInteger,
$table->bigInteger('sl_category_id')->unsigned()->nullable();
I'm adding this answer in case someone else will find some use of it.

Related

laravel errno: 150 "Foreign key constraint is incorrectly formed

I user below tutorial for laravel categories :
Laravel categories with dynamic deep paths
I use below code same tutorial for migration :
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title');
$table->string('slug');
$table->integer('parent_id')->unsigned()->default(0);
$table->timestamps();
});
Schema::table('categories', function (Blueprint $table) {
$table->foreign('parent_id')->references('id')->on('categories')->onUpdate('cascade')->onDelete('cascade');
});
}
but I have below error :
SQLSTATE[HY000]: General error: 1005 Can't create table 'xxx'.'#sql-453_147' (errno: 150 "Foreign key constraint is incorrectly formed")
(SQL: alter table 'categories' add constraint 'categories_parent_id_foreign' foreign key ('parent_id') references 'categories' ('id') on delete cascade on update cascade)
Thanks for help
When creating a new table in Laravel. A migration will be generated like:
$table->bigIncrements('id');
Instead of (in older Laravel versions):
$table->increments('id');
When using bigIncrements the foreign key expects a bigInteger instead of an integer. So your code will look like this:
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('title');
$table->string('slug');
$table->bigInteger('parent_id')->unsigned()->default(0);
$table->timestamps();
});
Schema::table('categories', function (Blueprint $table) {
$table->foreign('parent_id')->references('id')->on('categories')->onUpdate('cascade')->onDelete('cascade');
});
}
The problem is because of the difference of the column types. So unlike the tutorial you are using bigIncrements which means big integer for the ID and using the default integer for the parent_id. Try changing the id to this:
$table->increments('id');
or your parent_id to this:
$table->bigInteger('parent_id')->unsigned()->default(0);

SQLSTATE[HY000]: General error: 1215 - Even with 'InnoDB' and unsigned

I'm trying to create a foreign key on the country_id field from domains table to the id field of countries. But when I do the migration: php artisan migrate:fresh.
Error message:
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `domains` add constraint `domains_country_id_foreign` foreign key (`country_id`) references `countries` (`id`) on delete cascade)
Here is the domains migration file:
public function up()
{
Schema::create('domains', function (Blueprint $table) {
$table->increments('id');
$table->string('code');
$table->string('name');
$table->string('display_name');
$table->string('extension');
$table->string('google_analytics')->nullable();
$table->string('google_webmastertool')->nullable();
$table->string('google_maps')->nullable();
$table->integer('country_id')->unsigned();
$table->boolean('actif')->default(true);
$table->timestamps();
$table->engine = 'InnoDB';
});
Schema::table('domains', function (Blueprint $table) {
$table->foreign('country_id')->references('id')->on('countries')->onDelete('cascade');
});
}
And here is the countries migration file:
public function up()
{
Schema::create('countries', function (Blueprint $table) {
$table->increments('id');
$table->string('code');
$table->string('name');
$table->string('alpha2', 2);
$table->string('alpha3', 3);
$table->timestamps();
$table->engine = 'InnoDB';
});
}
As you can see the country_id field is unsigned and the table engine is InnoDB. I have already done other migrations with foreign key and they work fine, but this one, doesn't work :|
Thanks in advance for your help!
Solution from #Bogdan.
Change the order of the migration with the timestamp of the migration file.
You need to have the file with foreign key have a highter timestamp than the timestamp of the migration file where the primary key is located.

Foreign Key makes Problems in Laravel 5.6

I would like to program an inventory system and need 3 tables for it. These can also be generated via artisan without a foreign key. But as soon as I want to add a foreign key, I get the following error message.
SQLSTATE[HY000]: General error: 1005 Can't create table `inventar`.`#sql-fd4_141` (errno: 150 "Foreign key constraint is incorrectly formed")
(SQL: alter table items add constraint items_lend_foreign foreign key (lend) references lending (id))
Here my Code:
Item Table
Schema::create('items', function (Blueprint $table) {
$table->string('barcode');
$table->primary('barcode');
$table->string('name');
$table->string('description')->nullable();
$table->string('room')->nullable();
$table->string('status')->nullable();
$table->string('annotation')->nullable();
$table->string('image')->nullable();
$table->integer('lend')->unsigned()->nullable();
$table->string('manufactor')->nullable();
$table->timestamps();
});
Schema::table('items',function ($table){
$table->foreign('lend')->references('id')->on('lending');
});
Lending Table
Schema::create('lending', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->integer('personid')->unsigned();
$table->dateTime('startdate');
$table->dateTime('enddate');
$table->timestamps();
});
Schema::table('lending',function ($table){
$table->foreign('barcode')->references('barcode')->on('items');
$table->foreign('personid')->references('personid')->on('persons');
});
Persons-Table
Schema::create('persons', function (Blueprint $table) {
$table->integer('personid')->unsignd()->primary();
$table->string('firstname');
$table->string('lastname');
$table->string('email');
$table->string('annotation')->nullable();
$table->timestamps();
});
I've also googled, but found no solution that works for me.
Is it a problem that my primary key is a string?
Your problem comes in the line
Schema::create('persons', function (Blueprint $table) {
$table->integer('personid')->unsignd()->primary();
}
Change it to unsigned() from unsignd() and it will work fine.

errno: 150 "Foreign key constraint is incorrectly formed Laravel

Article migration
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->integer('people_id');
$table->string('title');
$table->timestamps();
$table->foreign('author_id')->references('id')->on('people');
});
People Migration
Schema::create('people', function (Blueprint $table) {
$table->increments('id');
$table->string('first_name');
$table->string('last_name');
$table->string('twitter');
$table->timestamps();
});
Error
SQLSTATE[HY000]: General error: 1005 Can't create table `article`.`#sql-1204_c` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter tab
le `articles` add constraint `articles_author_id_foreign` foreign key (`author_id`) references `people` (`id`))
i dont know what im doing wrong, both entity is consistent for me, i do not know why im receiving error 150
First make your people_id unsigned by-
$table->integer('people_id')->unsigned();
Change this line-
$table->foreign('author_id')->references('id')->on('people');
To this-
$table->foreign('people_id')->references('id')->on('people');
Check the order of your migrations. Run the people table migration first then articles table migration.
It's because you have not defined the author_id field
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('author_id');
$table->string('title');
$table->timestamps();
$table->foreign('author_id')->references('id')->on('people');
});

Error in adding foreign key in laravel

I am beginner in laravel. I am having problem in adding foreign key in laravel. I tried but I can't find the error.
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1005 Can't create table laravel_home1
#sql-4b8_2a (errno: 150 "Foreign key constraint is incorrectly formed")
(SQL: alter table `posts` add constraint
`posts_comment_id_foreign` foreign key (`comment_id`) references
`comments` (`id`))
post table migration code
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->string('content');
$table->integer('comment_id')->unsigned();
});
Schema::table('posts', function ($table) {
$table->foreign('comment_id')
->references('id')
->on('comments');
});
}
comments table migration code
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->string('comment');
$table->timestamps();
});
}
public function down()
{
Schema::drop('comments');
}
Problems with adding foreign key in laravel come with 3 reasons:
You have already done some rollbacks or migrated the same migration file multiple times and they are saved in cache and thus causing the problem
You forgot to put ->unsigned(); at the end of that index.
The table with primary key which is later used elsewhere as a foreign key is not migrated previously.
Either way try to put ->unsigned(); at the end like this
$table->integer('item_id')->unsigned();
$table->foreign('item_id')->references('id')->on('items');
and then run composer dump-autoload
and then php artisan migrate
and this should work!
UPDATE
I have found what is wrong with your migration:
Replace this:
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->string('content');
$table->integer('comment_id')->unsigned();
});
Schema::table('posts', function ($table) {
$table->foreign('comment_id')
->references('id')
->on('comments');
});
}
With this:
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->string('content');
$table->integer('comment_id')->unsigned();
$table->foreign('comment_id')
->references('id')
->on('comments');
});
}

Resources