Foreign key constraint cannot add - php

SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table products add constraint products_brand_id_foreign foreign key (brand_id) references product_brands (id) on delete cascade)
This is my brands migration
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateProductBrandsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('product_brands', function (Blueprint $table) {
$table->id();
$table->string('brand_name');
$table->string('brand_status');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('product_brands');
}
}
and this is my products migration
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateProductsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->string('product_name');
$table->string('sku');
$table->string('product_unit_price');
$table->string('quantity');
$table->string('product_image');
$table->string('product_description');
$table->integer('brand_id')->unsigned();
$table->integer('category_id')->unsigned();
$table->integer('unit_id')->unsigned();
$table->string('vat_type');
$table->timestamps();
});
Schema::table('products', function (Blueprint $table) {
$table->foreign('brand_id')->references('id')->on('product_brands')->onDelete('cascade');
$table->foreign('category_id')->references('id')->on('product_categories')->onDelete('cascade');
$table->foreign('unit_id')->references('id')->on('units')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('products');
}
}

Related

(errno: 150 "Foreign key constraint is incorrectly formed") in laravel 9 migration

I have two tables in my laravel application.
company table
employee table.
Following is my company table migration file.
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('companies', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->string('logo');
$table->text('email', 50)->unique();
$table->string('website');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('companies');
}
};
and the following is the employee table.
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('employees', function (Blueprint $table) {
$table->id();
$table->string('first_name');
$table->string('last_name');
$table->text('email', 50)->unique();
$table->text('phone', 50)->unique();
$table->integer('company_id')->unsigned();
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('employees');
}
};
But,
Whenever I tried to run my employee table migration, it kept giving me an error saying,
SQLSTATE[HY000]: General error: 1005 Can't create table
laraapp.employees (errno: 150 "Foreign key constraint is
incorrectly formed") (SQL: alter table employees add constraint
employees_company_id_foreign foreign key (company_id) references
companies (id) on delete cascade)
ID of my company table is UNSIGNED.
The value of $table->id(); is actually unsignedBigInteger() but you've defined your company_id foreign key column to only be an unsignedInteger so the two column types do no match.
As you are using Laravel 9, you can use the foreignId() method which will automatically create the column with the correct type:
$table->foreignId('company_id');
You could also use the foreignIdFor() method which will also create the underly required fk constraint so you don't have to.
$table->foreignIdFor(Company::class);
For completeness:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('employees', function (Blueprint $table) {
$table->id();
$table->string('first_name');
$table->string('last_name');
$table->text('email', 50)->unique();
$table->text('phone', 50)->unique();
$table->foreignIdFor(Company::class);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('employees');
}
};

SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (Laravel)

I keep getting this error. I understood it's a problem with the foreign key but i really can't understand what it's not working properly. I'm trying to make a many to many relationship with a bridge table. Please, if you can help me solve this! Here you can see the migrations:
Table dresses:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class Dress extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('dresses', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('dress_id');
$table->string('name')->unique();
$table->string('brand');
$table->decimal('price');
$table->string('fabric');
$table->string('size');
$table->string('type');
$table->string('wash_instruction');
$table->string('product_details');
$table->string('fit');
$table->rememberToken();;
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
//
}
}
Table Wishlist:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class Wishlist extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('wishlists', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('wishlist_id');
$table->foreign('wishlist_id')
->references('user_id')
->on('users')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
//
}
}
Bridge table:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateWishlistDressTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('wishlist_dress', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('dress_id');
$table->foreign('dress_id')
->references('id')
->on('dresses')->onDelete('cascade');
$table->unsignedBigInteger('wishlist_id');
$table->foreign('wishlist_id')
->references('id')
->on('wishlists')->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('wishlist_dress');
}
}
How is your users migration set up? Do you have a column named users_id in the users migration file?
Usually, only the id column is used, which is generated from $table->id() in the users migrations file. I believe your migrations should work if you change from users_id to id like following:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class Wishlist extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('wishlists', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('wishlist_id');
$table->foreign('wishlist_id')
->references('id') //Changed from user_id to id
->on('users')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
//
}
}

can't delete or update item in laravel

when i tried delete item in laravel i get this message
SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (larblog.comments, CONSTRAINT comments_article_id_foreign FOREIGN KEY (article_id) REFERENCES articles (id)) (SQL: delete from articles where id = 2)
this my delete function
public function DeleteArticle($id){
$article = Article::find($id);
$article->delete();
return redirect("article");
}
this create articles table code
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateArticle extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('articles', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->text('body');
$table->timestamps();
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('articles');
}
}
and this create comments table code
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateComments extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->text('comment');
$table->timestamps();
$table->integer('article_id')->unsigned();
$table->foreign('article_id')->references('id')->on('articles');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('comments');
}
}
Iam tried to use this solution but did not work
$table->foreign('article_id')->references('id')->on('articles')->onUpdate('cascade')-
>onDelete('cascade');
as you know your comments depends on articles once a article is delete then comment's relation going to break so either you first delete all comment's of that artical or set foreign key null to all the comment for that article
so that why you need to update your mygration with
$table->foreign('article_id')->references('id')->on('articles')->onDelete('cascade');
or
$table->integer('article_id')->unsigned()->nullable();
$table->foreign('article_id')->references('id')->on('articles')->onDelete('set null');

Problem with error: 1215 Cannot add foreign key constraint

How to solve this error? SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table invoices add constraint invoices_form_id_foreign foreign key (form_id) references forms (id) on delete cascade)
I get it after: php artisan migrate:fresh
For foreign keys in invoices get this error. 3 foreign keys generate error but one for user_id is good and works fine. I tried all solutions and didn't worked. Please help me.
2014_10_12_000000_create_users_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateUsersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('surname')->nullable();
$table->string('showname')->nullable();
$table->string('business')->nullable();
$table->string('NIP')->nullable();
$table->string('PESEL')->nullable();
$table->string('address')->nullable();
$table->string('city')->nullable();
$table->string('postalcode')->nullable();
$table->string('phone')->nullable();
$table->string('comments')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('users');
}
}
2020_07_23_104440_invoices.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class Invoices extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('invoices', function (Blueprint $table) {
$table->increments('id');
$table->string('invoicenumber')->nullable();
$table->date('invoicedate')->nullable();
$table->date('selldate')->nullable();
$table->integer('user_id')->unsigned()->nullable();
$table->integer('form_id')->unsigned()->nullable();
$table->integer('currency_id')->unsigned()->nullable();
$table->integer('proform_id')->unsigned()->nullable();
$table->string('paymentmethod')->nullable();
$table->date('paymentdate')->nullable();
$table->string('status')->nullable();
$table->string('comments')->nullable();
$table->string('city')->nullable();
$table->string('paid')->nullable();
$table->string('autonumber')->nullable();
$table->string('automonth')->nullable();
$table->string('autoyear')->nullable();
$table->timestamps();
});
Schema::table('invoices', function (Blueprint $table){
$table->foreign('user_id')
->references('id')
->on('users');
$table->foreign('form_id')
->references('id')
->on('forms');
$table->foreign('currency_id')
->references('id')
->on('currencys');
$table->foreign('proform_id')
->references('id')
->on('proforms');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('invoices');
}
}
2020_07_27_090356_proforms.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class Proforms extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('proforms', function (Blueprint $table) {
$table->increments('id');
$table->string('proformnumber')->nullable();
$table->date('proformdate')->nullable();
$table->date('selldate')->nullable();
$table->integer('user_id')->unsigned()->nullable();
$table->string('paymentmethod')->nullable();
$table->date('paymentdate')->nullable();
$table->string('status')->nullable();
$table->string('comments')->nullable();
$table->timestamps();
});
Schema::table('proforms', function (Blueprint $table){
$table->foreign('user_id')
->references('id')
->on('users');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('proforms');
}
}
2020_07_28_091856_forms.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class Forms extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('forms', function (Blueprint $table) {
$table->increments('id');
$table->string('form')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('forms');
}
}
2020_07_28_091919_currencys.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class Currencys extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('currencys', function (Blueprint $table) {
$table->increments('id');
$table->string('currency')->nullable();
$table->string('course')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('currencys');
}
}
Here is what might be happening with your migrations.
1. Check the the primary and foreign key type. If user primary key ID is type $table->increments('id'); // bigInteger, then the foreign key should be bigInteger as well $table->bigInteger('user_id');
2. Please check your migration order. For example table invoices has foreign key fromforms table. During the migration, forms table should migrate first. And to make it make it happen, just rename the migration file(numeric section of file name) so that the forms.
because when excuting the migration reach this line:
$table->foreign('form_id')->references('id')->on('forms');
in 2020_07_23_104440_invoices file
the forms table has not been created yet
you should make a new migration to make that relation
Schema::table('invoices', function (Blueprint $table){
$table->foreign('form_id')
->references('id')
->on('forms');
}
this migration should be after 2020_07_28_091856_forms.php migration ...
The problem is the order of the migration
see the order
2020_07_23_104440_invoices.php
2020_07_28_091856_forms.php
when the invoices table is created there will be no forms table as it is created afterwards. So the error is because you are trying to create relationship between non existing forms table id field while creating invoices table.
What you can do is create a new migration file for attaching the relationship only.
here is how to do it
delete add foreign key from. remove this part from 2020_07_23_104440_invoices.php
$table->foreign('form_id')
->references('id')
->on('forms');
after removing create seperate migration for adding forign key
run
php artisan make:migration add_form_foreign_key_to_invoices
It will generate the new migration file. Update it like this.
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddFormForeignKeyToInvoices extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('invoices', function (Blueprint $table) {
$table->foreign('form_id')->references('id')->on('forms');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('invoices', function (Blueprint $table) {
$table->dropForeign('form_id');
});
}
}

Laravel Migrations: Cannot add foreign key constraint

I am trying to add foreign key constraints to my database tables via Laravel Migrations, but I always get an error like this:
Illuminate\Database\QueryException :
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
(SQL: alter table `tasks` add constraint `tasks_task_list_id_foreign` foreign key (`task_list_id`) references `task_lists` (`id`))
The migration for the tasks table looks like this:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTasksTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('tasks', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned()->index();
$table->integer('task_list_id')->unsigned()->index();
$table->string('task');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('task_list_id')->references('id')->on('task_lists');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('tasks');
}
}
And that's the task_lists table:
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateTaskListsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('task_lists', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned()->index();
$table->string('name');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('task_lists');
}
}
I can't figure out the problem and would highly appreciate any kind of help.
Thank you in advance!
You have to check the order of your migrations. Obviously, you have to run de Users table migration before the Tasks table migration. If the problem persist, try to make something like this inside the up() method and after the (Schemma::create):
Schema::table('task_lists', function($table){
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});

Categories