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.
Related
I am having migrations issue. Table is below.
public function up()
{
Schema::create('users_articles_likes', function (Blueprint $table) {
// $table->increments('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->integer('article_id')->unsigned();
$table->foreign('article_id')->references('id')->on('articles');
$table->primary(['user_id', 'article_id']);
$table->timestamps();
});
}
When I try to migrate it. It doesn't push the whole table. Just pushes the user_id and article_id
and this the error I am displaying in terminal.
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
(SQL: alter table users_articles_likes add constraint
users_articles_likes_user_id_foreign foreign key (user_id)
references users (id))
User table
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
So the problem is because of wrong data type.. In your users table you have bigIncrements which is Big Integer data type, and in the other table integer
So try this:
$table->bigInteger('user_id')->unsigned();
// or
$table->unsignedBigInteger('user_id');
make sure you check the article_id too.
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);
I am trying to execute this migration :
Users :
Schema::create('comments', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('first_name');
$table->string('last_name');
$table->string('email')->unique();
$table->string('avatar_url');
$table->string('email_verified_at')->nullable();
$table->string('password')->unique();
$table->rememberToken();
$table->timestamps();
$table->softDeletes();
});
Articles :
Schema::create('articles', function (Blueprint $table) {
$table->bigIncrements('id');
$table->text('title');
$table->longText('body');
$table->enum('state', ['draft', 'published']);
$table->bigInteger('user_id')->unsigned();
$table->timestamps();
$table->softDeletes();
$table->foreign('user_id')
->references('id')->on('users')
->onUpdate('cascade')->onDelete('cascade');
});
But when I migrate I get the following error :
SQLSTATE[HY000]: General error: 1005 Can't create table blog_api.#sql-2b70_7b (Errcode: 150 "Foreign key constraint is inc
orrectly formed") (SQL: alter table articles add constraint articles_user_id_foreign foreign key (user_id) references users
(id) on delete cascade on update cascade)
I already tried to rename big integer and big increments to simple integer and increments withouth success.
There should be one unassignedBigInteger and then you set your foreign key.
https://laravel.com/docs/5.8/migrations#foreign-key-constraints Please check the official documentation
Besides,
Please be carefull with order of migration. It starts from the first file through to latest one, so if you are trying to set foreign key for the table hasn't been created yet, it will throw an error.
E.g
Users table has foreign key relation with articles and like following;
create_users_table
create_articles_table
Since articles table not created yet, you will not be able to assign. For such cases like this, i suggest you to use "add_foreign_keys_to_articles" after all basic structure of tables created.
Schema::table('articles', function(Blueprint $table)
{
$table->foreign('user_id')
->references('id')->on('users')
->onUpdate('cascade')->onDelete('cascade');
});
I have a ‘posts’ table and an ‘arrival’ table which references ‘flightno’ (in text string format) as a foreign key. However, when I run the Laravel migration I get the dreaded error:
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1005 Can't create table atc.#sql-2350_84 (errno: 150 "Foreign key constraint is
incorrectly formed") (SQL: alter table arrival add constraint
arrival_flightno_foreign foreign key (flightno) references posts
(flightno))
[PDOException]
SQLSTATE[HY000]: General error: 1005 Can't create table atc.#sql-2350_84 (errno: 150 "Foreign key constraint is
incorrectly formed")
Posts
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('flightno');
$table->string('flighttype');
$table->string('toa');
$table->string('doa');
$table->string('runway');
$table->string('route');
$table->string('parking');
$table->timestamps();
});
Arrival
Schema::create('arrival', function (Blueprint $table) {
$table->increments('id');
$table->string('flightno');
$table->string('cleaning');
$table->string('rampservice');
$table->string('waste');
$table->string('deicing');
$table->foreign('flightno')->references('flightno')->on('posts')->onDelete('cascade');
$table->timestamps();
});
Seems to me that you forgot to put indexes and set length of flightno columns. This should work:
Posts
Schema::create('posts', function (Blueprint $table) {
// ...
$table->string('flightno', 30)->index();
// ...
});
Arrival
Schema::create('arrival', function (Blueprint $table) {
// ...
$table->string('flightno', 30)->index();
// ...
});
In the foreign key column use unsignedBigInteger to avoid mismatch foreign key data type problem.
For example in your arrival table use the type of flightno as unsignedBigInteger.
Posts:
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('flightno');
$table->string('flighttype');
$table->string('toa');
$table->string('doa');
$table->string('runway');
$table->string('route');
$table->string('parking');
$table->timestamps();
});
Arrival:
Schema::create('arrival', function (Blueprint $table) {
$table->increments('id');
//---Change flightno type to unsignedBigInteger and if u faced any problem just use nullable at the end of flightno.
$table->unsignedBigInteger('flightno');
$table->string('cleaning');
$table->string('rampservice');
$table->string('waste');
$table->string('deicing');
$table->foreign('flightno')->references('flightno')->on('posts')->onDelete('cascade');
$table->timestamps();
});
It will solved the problems of a lot of people that faced this problem.
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');
});