Laravel 4 Migration - Cannot add foreign key constraint - laravel

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');

Related

Laravel migration: integrity constraint violation

I get an error whenever I try to insert values into this table.
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or
update a child row: a foreign key constraint fails
(usersexam.role_user, CONSTRAINT role_user_role_id_foreign
FOREIGN KEY (role_id) REFERENCES roles (id) ON DELETE CASCADE)
Here is the migration for that table in my Laravel's database folder:
class CreateRoleUserPivotTable extends Migration
{
public function up()
{
Schema::create('role_user', function (Blueprint $table) {
$table->foreignId('role_id')->references('id')->on('roles')
->cascadeOnDelete();
$table->foreignId('user_id')->references('id')->on('users')
->cascadeOnDelete();
});
}
public function down()
{
Schema::dropIfExists('role_user');
}
}
So I am inserting it into two columns. What is wrong with the table?
UPDATE: here is my insert query
when ever ur creating a tables and refrense a foreign key u still need to make the columns so instead of
Schema::create('role_user', function (Blueprint $table) {
$table->foreignId('role_id')->references('id')->on('roles')->cascadeOnDelete();
$table->foreignId('user_id')->references('id')->on('users')->cascadeOnDelete();
});
it should be something like
Schema::create('role_user', function (Blueprint $table) {
$table->unsignedInteger('role_id');
$table->unsignedInteger('user_id');
$table->foreign('role_id')
->references('id')
->on('roles')
->cascadeOnDelete();
$table->foreign('user_id')
->references('id')
->on('users')
->cascadeOnDelete();
});

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 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.

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.

Resources