How to create 2 foreign keys with Laravel migration - laravel-5

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

Related

1005 Can't create table `portal`.`employees` (errno: 150 "Foreign key constraint is incorrectly formed")")

Hey guys I tried too much stuff and read some blogs or discussion I didn't fix my problem I'm new in laravel this project. I got error when I want to create to database this error like
PDOException::("SQLSTATE[HY000]: General error: 1005 Can't create table `portal`.`employees` (errno: 150 "Foreign key constraint is incorrectly formed")")
my migration for payments:
public function up()
{
Schema::create('payments', function (Blueprint $table) {
$table->bigInteger('id');
$table->unsignedBigInteger('employee_id');
$table->decimal('salary',16);
$table->decimal('amount_paid',16);
$table->unsignedBigInteger('paid_by');
$table->string('remark');
$table->string('department',50);
$table->timestamps();
});
Schema::table('payments', function($table) {
$table->foreign('employee_id')->references('id')->on('employees')->onUpdate('cascade')->onDelete('cascade');
$table->foreign('paid_by')->references('id')->on('users');
});
}
my migration for employees:
public function up()
{
Schema::create('employees', function (Blueprint $table) {
$table->unsignedBigInteger('id');
$table->string('name');
$table->string('department');
$table->string('location');
$table->string('telephone');
$table->decimal('salary',16);
$table->string('cover_image');
$table->foreign('department')->references('name')->on('departments')->onUpdate('cascade')->onDelete('cascade');
$table->timestamps();
});
Department Table:
public function up()
{
Schema::create('departments', function (Blueprint $table) {
$table->bigInteger('id');
$table->string('name');
$table->timestamps();
});
}
my migration folder:
https://i.stack.imgur.com/pCsbg.png
making foreign keys on non unique columns is not a good idea, and only innodb for mysql support that, see mysql doc.
instead of:
$table->foreign('department')->references('name')->on('departments')->onUpdate('cascade')->onDelete('cascade');
you should make the foreign key regularly like:
$table->bigInteger('department_id');
$table->foreign('department_id')->references('id')->on('departments')->onUpdate('cascade')->onDelete('cascade');
You can simply set the foreign key on the department Id and of course, it's much better and has better performance setting a foreign key on unique keys.

Foreign key is incorectly formed in Laravel

I have the following two migration files:
Schema::create('words', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->integer('book_id')->unsigned()->nullable();
$table->foreign('book_id')->references('id')->on('books')->onDelete('cascade');
$table->string('title');
$table->longText('definition', 2056);
$table->timestamps();
});
and:
Schema::create('books', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->integer('word_id')->unsigned()->nullable();
$table->foreign('word_id')->references('book_id')->on('words');
$table->string('title');
$table->string('author')->nullable();
$table->date('start_date')->nullable();
$table->date('end_date')->nullable();
$table->integer('no_pages')->nullable();
$table->integer('current_page')->nullable();
$table->integer('status')->nullable();
$table->timestamps();
});
When I run the migrate command, the following error occur:
Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1005 Ca
n't create table `words`.`#sql-16bc_76` (errno: 150 "Foreign key constraint is i
ncorrectly formed") (SQL: alter table `books` add constraint `books_word_id_fore
ign` foreign key (`word_id`) references `words` (`book_id`))
Why is this happens? I've added word_id into books table so I can count the words added for that book.
The problem could be that the tables that contain the id that is being used as a foreign id needs to be created before another table can reference it. Basically, you are creating a table and telling MySQL to reference another table's primary key but that table doesn't exist yet. Try to create them separately.

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

Laravel migration Error no: 150 "Foreign key constraint is incorrectly formed"

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.

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