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();
Related
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');
});
}
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.
i want to add two foreign key in one Model but its seem doesnt work and return Error like this
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table expenses add constraint expenses_shop_id_foreign foreign key (shop_id) references shops (id))
and below is my Model
<pre>
Schema::create('expenses', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('user_id');
$table->unsignedBigInteger('shop_id');
$table->date('date');
$table->string('description');
$table->double('amount');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
$table->foreign('shop_id')->references('id')->on('shops');
});
</pre>
My shop Model
<pre>
Schema::create('shops', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('description');
$table->timestamps();
});
</pre>
is there any way to make it work? Thanks in advances...
unsignedBigInteger will create unsigned integer in database but bigIncrements will create signed integer so you need to make them the same type.
either you change:
shops table:
$table->bigIncrements('id');
expenses table:
$table->BigInteger('shop_id');
or :
shops table:
$table->increments('id')
expenses table:
$table->unsignedInteger('shop_id');
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.
Here's my migration code:
public function up()
{
Schema::create('foos', function(Blueprint $table) {
// Primary key
$table->increments('id');
// Standard
$table->engine = 'InnoDB';
$table->timestamps();
$table->softDeletes();
});
Schema::create('bars', function(Blueprint $table) {
// Primary key
$table->increments('id');
// Define foreign key
$table->integer('foo_id')->unsigned;
// Foreign key contraints
// NOTE: causes "General error: 1215 Cannot add foreign key constraint"
// $table->foreign('foo_id')->references('id')->on('foos');
// Standard
$table->engine = 'InnoDB';
$table->timestamps();
$table->softDeletes();
});
}
public function down()
{
Schema::drop('foos');
Schema::drop('bars');
}
When the code to define the foreign key constraint is not commented out, I get the following error on the command line: General error: 1215 Cannot add foreign key constraint.
Any ideas what I am doing wrong?
$table->integer('foo_id')->unsigned;
should be
$table->integer('foo_id')->unsigned();
or you can use short version:
$table->unsignedInteger('foo_id');