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

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.

Related

General error: 1215 Cannot add foreign key constraint - laravel migrations

I'm trying to add a couple of foreign keys to a pivot table, like thus:-
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedInteger('user_id');
$table->string('author');
$table->string('title');
$table->longText('content');
$table->timestamps();
$table->engine = 'InnoDB';
});
}
public function up()
{
Schema::create('tags', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->timestamps();
$table->engine = 'InnoDB';
});
}
public function up()
{
Schema::create('post_tag', function (Blueprint $table) {
$table->primary(['post_id', 'tag_id']);
$table->bigInteger('post_id');
$table->bigInteger('tag_id');
// $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
// $table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade');
$table->timestamps();
$table->engine = 'InnoDB';
});
Schema::table('post_tag', function($table) {
$table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');
$table->foreign('tag_id')->references('id')->on('tags')->onDelete('cascade');
});
}
I run the migrations and receive an error like thus:-
In Connection.php line 692:
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL
: alter table `post_tag` add constraint `post_tag_post_id_foreign` foreign
key (`post_id`) references `posts` (`id`) on delete cascade)
In Connection.php line 485:
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
Looking around the net and I'm led to believe its an integrity violation between primary/foreign key datatypes. Like, a foreign key with an integer data type can't reference a primary key whose type is of bigInt. But, in my migration, I'm ensuring the types are the same. Alas, no matter, I still get the error and my migrations fail to compile?
Help
bigIncrements are big unsigned integer
$table->bigIncrements('id');
change the tag_id, post_id field type to unsigned
$table->unsignedBigInteger('post_id');
$table->unsignedBigInteger('tag_id');
As you did with the user_id in the posts table.
The bigIncrements() method creates an auto-incrementing UNSIGNED BIGINT (primary key) equivalent column, so you need to define unsigned also for foreign key, because foreign key need to be same type as your primary key :
$table->unsignedBigInteger('post_id');
$table->unsignedBigInteger('tag_id');
// or
$table->bigInteger('post_id')->unsigned();
$table->bigInteger('tag_id')->unsigned();

Laravel migration: adding foreign key to the same table where ID is a string

I'm trying to make "categories" migration, where each category refers it's parent category by ID in the same table.
Migration:
Schema::create('categories', function (Blueprint $table) {
$table->string('id', 36)->primary();
$table->string('parent_id', 36)->nullable();
$table->foreign('parent_id')->references('id')->on('categories');
$table->string('name');
});
But i'm getting next error:
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table categories add constraint categories_parent_id_foreign foreign key (parent_id) references categories (id))
Field types are the same, and I don't know, what to do.
Removing "->nullable()" has no effect.
Laravel Framework version 6.20.7
Thanks.
Add the foreign key constraint in another run like as below
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->string('id', 36)->primary();
$table->string('parent_id', 36)->nullable();
$table->string('name');
});
Schema::table('categories',function (Blueprint $table){
$table->foreign('parent_id')->references('id')->on('categories');
});
}

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

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.

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.

How to create 2 foreign keys with Laravel migration

I have letter's model and this model has 2 foreign keys from attachments and contacts
public function up()
{
Schema::create('letters', function (Blueprint $table) {
$table->increments('id');
$table->integer('contact_id')->unsigned();
$table->integer('attachment_id')->unsigned();
$table->timestamps();
$table->foreign('contact_id')->references('id')->on('contacts');
$table->foreign('attachment_id')->references('id')->on('attachments');
});
}
This is what I have tried so far. But when I type artisan migration SQL I receive the following error
General error: 1005 Can't create table ss.#sql-1064_4a (errno: 150 "Foreign
key constraint is incorrectly formed") (SQL: alter table letters add constraint letters_attach
ment_id_foreign foreign key (attachment_id) references attachments (id))
first create table rows then add forgin keys.
Schema::create('letters', function (Blueprint $table) {
$table->increments('id');
$table->integer('contact_id')->unsigned();
$table->integer('attachment_id')->unsigned();
$table->timestamps();
});
Schema::table('letters', function (Blueprint $table) {
$table->foreign('contact_id')->references('id')->on('contacts');
$table->foreign('attachment_id')->references('id')->on('attachments');
});
i guess this will work

Resources