So I'm creating a migration file following the template of my old one as follows:
create_banners_table (this one WORKED)
class CreateBannersTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('banners', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('order');
$table->string('published');
$table->timestamps();
});
Schema::create('banner_translations', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->string('locale', 10)->index();
$table->string('title');
$table->string('url');
$table->foreignId('banner_id')
->constrained()
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('banner_translations');
Schema::dropIfExists('banners');
}
}
Here's the create_managements_table (this one FAILED):
class CreateManagementsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('managements', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('order');
$table->string('published');
$table->string('name');
$table->string('category')
->comment('board of commissioners', 'board of executives');
$table->timestamps();
});
Schema::create('management_translations', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->string('locale', 10)->index();
$table->string('description');
$table->string('title');
$table->foreignId('management_id')
->constrained()
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('management_translations');
Schema::dropIfExists('managements');
}
}
The problem is, when I tried to make another migration file based on that, I got this error:
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `management_translations` add constraint `management_translations_management_id_foreign` foreign key (`management_id`) references `management` (`id`) on delete cascade)
As you can see both of the translation table references the big integer in their respective base table (so there is no type mismatch).
For further investigation, I ran SHOW ENGINE INNODB STATUS;
and checked the Latest Foreign Key Error and got the following:
------------------------
LATEST FOREIGN KEY ERROR
------------------------
2021-08-28 12:01:41 0x7f6f44091700 Error in foreign key constraint of table cemindo/#sql-200_7:
foreign key (`management_id`) references `management` (`id`) on delete cascade:
Cannot resolve table name close to:
(`id`) on delete cascade
Whether or not the word management countable is ambiguous so apparently Laravel treats it as uncountable in this case. That's to say that Laravel thinks the corresponding table name for management_id would be management not managements which is an uncommon plural form. Therefore you either should rename your base table to management or use the following foreign key sytnax:
Schema::create('management_translations', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->string('locale', 10)->index();
$table->string('description');
$table->string('title');
$table->foreignId('management_id')
->constrained('managements')
->onDelete('cascade');
});
Schema::create('banner_translations', function (Blueprint $table) {
$table->increments('banner_id')->unsigned(); //change this line
$table->string('locale', 10)->index();
$table->string('title');
$table->string('url');
$table->foreignId('banner_id')
->constrained()
->onDelete('cascade');
});
I think you should provide banner_id first and then provide foreignId.(same column name)
you can add foreign key reference as like below,
first create all the keys then at last declare the foreign key reference ... you can see this method at Laravel Doc's ... See First example of that link
Schema::create('managements', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('order');
$table->string('published');
$table->string('name');
$table->string('category')
->comment('board of commissioners','board of executives');
$table->timestamps();
});
Schema::create('management_translations', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->string('locale', 10)->index();
$table->string('description');
$table->string('title');
$table->unsignedBigInteger('management_id');
$table->foreign('management_id')
->references('id')
->on('managements');
->onDelete('cascade');
});
If you face any error using my solution let me know, I will update my answer
Thank you
Im trying to migrate my tags tables but i keep getting this error:
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))
when running:
php artisan migrate:fresh
here is my code:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreatePostTagTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('post_tag', function (Blueprint $table) {
$table->bigIncrements('id');
$table->integer('post_id')->unsigned();
$table->integer('tag_id')->unsigned();
$table->foreign('post_id')->references('id')->on('posts');
$table->foreign('tag_id')->references('id')->on('tags');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('post_tag');
}
}
CreateTagsTable:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateTagsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('tags', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('tags');
}
}
How do i fix it?
Im currently reffering a ecommerce lesson using laravel 8.when creating categories for the products im keep getting this error message while migrating.
SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (SQL: drop table if exists products)
This is my create_products_table
<?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->increments('id');
$table->timestamps();
$table->string('name');
$table->string('slug');
$table->text('description');
$table->integer('price');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('products');
}
}
and here is create_category_product_table
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class CreateCategoryProductTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('category_product', function (Blueprint $table) {
$table->increments('id');
$table->integer('product_id')->unsigned()->nullable();
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
$table->integer('category_id')->unsigned()->nullable();
$table->foreign('category_id')->references('id')->on('category')->onDelete('cascade');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('category_product');
}
}
Much appreciate if someone can teach me to solve this. Thank You!
I've foreign key error in my HOOFDVRAAGS database but the strange this is that's the exact same as in VRAAGS
SQLSTATE[HY000]: General error: 1005 Can't create table
zonetoets.#sql-1 e8_2f9 (errno: 150 "Foreign key constraint is
incorrectly formed") (SQL: a lter table hoofdvraags add constraint
hoofdvraags_toets_id_foreign fore ign key (toets_id) references
toets (id) on delete cascade) In Connection.php line 458:
SQLSTATE[HY000]: General error: 1005 Can't create table
zonetoets.#sql-1 e8_2f9 (errno: 150 "Foreign key constraint is
incorrectly formed")
Hereby my db-migrations and hopefully someone can help me.
Hereby the relationships
TOETS 1---->N HOOFDVRAAGS 1 ----> N VRAAGS
HOOFDVRAAGS
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateHoofdvraagsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('hoofdvraags', function (Blueprint $table) {
$table->increments('id');
$table->integer('toets_id')->unsigned();
$table->string('titel');
$table->string('tekst');
$table->string('bestandsnaam');
$table->integer('volgnummerHoofdvraag')->default(99);
$table->timestamps();
$table->foreign('toets_id')
-> references('id')
-> on('toets')
-> onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('hoofdvraags');
}
}
TOETS
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateToetsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('toets', function (Blueprint $table) {
$table->increments('id');
$table->string('toetscode');
$table->string('jaargang');
$table->string('vak')->default('wiskunde');
$table->string('hoofdstuk');
$table->string('hoofdstuktitel');
$table->string('maker');
$table->string('datumgemaakt');
$table->integer('volgnummerToets')->default(1);
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('toets');
}
}
VRAAG
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateSubvraagsTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('subvraags', function (Blueprint $table) {
$table->increments('id');
$table->integer('hoofdvraag_id')->unsigned();
$table->string('vraag');
$table->string('antwoord');
$table->integer('punten');
$table->string('bestandsnaam');
$table->integer('volgnummerSubvraag')->default(1);
$table->timestamps();
$table->foreign('hoofdvraag_id')
-> references('id')
-> on('hoofdvraags')
-> onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('subvraags');
}
}
MODEL TOETS
public function hoofdvraag()
{
return $this->hasMany(Hoofdvraag::class);
}
MODEL HOOFDVRAAG
public function subvraag()
{
return $this->hasMany(Subvraag::class);
}
public function toets() {
return $this->belongsTo(Toets::class);
}
MODEL SUBVRAAG
public function hoofdvraag() {
return $this->belongsTo(Hoofdvraag::class);
}
as i know , if you are creating an eloquent realtionship inside the models why you are creating the foreign key inside the migration all you need is create a column for it.
https://laravel.com/docs/5.8/eloquent-relationships#one-to-many
I am getting this error from Laravel 5.4 SQL i am trying to make many foreign key in my one table product. Please help me. Why is it i am getting this error? i was looking and searching for other codes but their structure of making more than one foreign key is like my migration code.
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1005 Can't create table `store_db`.`#sql-ca4_1f1` (errno: 150 "Foreign key constrai
nt is incorrectly formed") (SQL: alter table `products` add constraint `products_product_img_id_foreign` foreign ke
y (`product_img_id`) references `product_images` (`product_img_id`) on delete cascade)
Here is my migration code:
Migration code for products table:
public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->increments('product_id')->unsigned();
$table->integer('category_id')->unsigned();
$table->integer('product_img_id')->unsigned();
$table->string('feature_img');
$table->string('product_name');
$table->text('description');
$table->double('price',15,2);
$table->integer('quantity');
$table->timestamps();
$table->foreign('category_id')
->references('category_id')->on('categories')
->onDelete('cascade');
$table->foreign('product_img_id')
->references('product_img_id')->on('product_images')
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('categories');
// Schema::drop('product_images');
Schema::drop('products');
}
Migration code for product_images table:
public function up()
{
Schema::create('product_images', function (Blueprint $table) {
$table->increments('product_img_id')->unsigned();
$table->string('product_img');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('product_images');
}
Migration code for categories table:
public function up()
{
Schema::create('categories', function (Blueprint $table) {
$table->increments('category_id')->unsigned();
$table->string('name');
$table->text('description');
$table->string('category_img');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('categories');
}
Any suggestions?