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
Related
The migration script below was running smoothly in an older version of Laravel but I added it to my fresh Laravel 5.8 and ran the script. I'm getting Error: foreign key was not formed correctly
Evaluation Migration:
public function up() {
Schema::create('evaluation', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned()->index();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
Users Migration:
public function up() {
Schema::create('users', function (Blueprint $table) {
$table->bigIncrements('id');
$table->timestamps();
});
}
As we discussed in the comments above, a foreign key column must be the same data type as the primary key it references.
You declared your user.id primary key as $table->bigIncrements('id') which becomes BIGINT UNSIGNED AUTO_INCREMENT in MySQL syntax.
You must declare the foreign key as $table->unsignedBigInteger('user_id') which will become BIGINT UNSIGNED in MySQL, making it compatible with being a foreign key to the user.id column.
update your `integer('user_id')` to `bigInteger('user_id')`
public function up() {
Schema::create('evaluation', function (Blueprint $table) {
$table->increments('id');
$table->bigInteger('user_id')->unsigned()->index();
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
});
}
I know this question has been ask a lot of time but i tried every solutions and it still give me an error.
I want to migrate my tables :
class CreateFichesTable extends Migration
{
public function up()
{
Schema::create('fiches', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id');
$table->string('type');
$table->string('nom');
$table->string('description');
$table->string('image');
$table->integer('equipe_id')->unsigned();
$table->timestamps();
});
Schema::table('fiches', function(Blueprint $table)
{
$table->foreign('equipe_id')->references('id')->on('equipes');
});
}
}
And
class CreateEquipesTable extends Migration
{
public function up()
{
Schema::create('equipes', function (Blueprint $table) {;
$table->increments('id');
$table->string('nom');
$table->string('image');
$table->timestamps();
});
}
}
And I get :
Exception trace:
1 PDOException::("SQLSTATE[HY000]: General error: 1215 Cannot add foreign
key constraint")
I tried to force using the engine "InnoDB" but still not working.
Any tip ?
When you create new migration file laravel automatically added in the start datetime like this 2018_08_10_111004_ for detect which migration must be create firstly. As you show in comments your migration files is 2018_08_31_141536_create_fiches_table.php and 2018_09_03_141649_create_equipes_table.php you must be change one of this migration for firstly called second migration the first. For example
2018_08_31_141536_create_fiches_table.php
2018_08_30_141649_create_equipes_table.php
tl;dr. Solution:
Thanks to Jonas.
the problem was that the tables I was referring as foreign, were not InnoDB.
I added raw SQL statements in alter migrations and then added the foreign keys:
DB::statement("ALTER TABLE table ENGINE='InnoDB';");
Original question
First, before the Stackoverflow police bust me, I know this question is probably 83% of the database of this website. But me is special (Kidding, I know I'm not). But I've tried most of the common stuff and nothing seems to work. So probably I'm overseeing something.
Error
General error: 1215 Cannot add foreign key constraint (SQL: alter table applications add constraint applications_user_id_foreign foreign key (user_id) references users (id) on delete cascade)
This is my migration:
public function up()
{
Schema::create("applications", function(Blueprint $table) {
$table->engine = "InnoDB";
$table->increments('id');
$table->timestamps();
});
Schema::table('applications', function($table) {
$table->integer('user_id')->unsigned()->index();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->integer('job_request_id')->unsigned()->index();
$table->foreign('job_request_id')->references('id')->on('job_requests')->onDelete('cascade');
$table->integer('status')->default(0);
});
}
What I've already tried:
1.
public function up()
{
Schema::create("applications", function(Blueprint $table) {
$table->increments('id');
$table->timestamps();
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->integer('job_request_id')->unsigned();
$table->foreign('job_request_id')->references('id')->on('job_requests')->onDelete('cascade');
$table->integer('status')->default(0);
});
}
2.
public function up()
{
Schema::create("applications", function(Blueprint $table) {
$table->engine = "InnoDB";
$table->increments('id');
$table->timestamps();
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->integer('job_request_id')->unsigned();
$table->foreign('job_request_id')->references('id')->on('job_requests')->onDelete('cascade');
$table->integer('status')->default(0);
});
}
Splitting the migration into two files (A create and an alter). Even adding each reference one by one.
4.- using DB::statement('SET FOREIGN_KEY_CHECKS=0;'); and =1 at the beginning an the end og the migration.
5.- removing the unsigned() and the index().
Might mean something:
1.- When I rollback the migration, it doesn't delete the table. So if I rollback and migrate, would give me a "already exists error".
2.- I already have migrations which reference the same items, i.e:
Schema::create('job_requests', function (Blueprint $table) {
...
$table->integer('user_id')->unsigned()->nullable();
$table->foreign('user_id')->references('id')->on('users');
...
});
UPDATE
For the drop methods I've tried:
For the create migrations
public function down()
{
Schema::drop('applications');
}
public function down()
{
Schema::dropIfExists('applications');
}
2.- For the alter migrations
public function down()
{
Schema::table('applications', function (Blueprint $table) {
$table->dropForeign(['user_id']);
$table->dropColumn('user_id');
$table->dropForeign(['job_request_id']);
$table->dropColumn('job_request_id');
});
}
UPDATE 2:
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->increments('id');
$table->string('first_name');
$table->string('last_name')->nullable();
$table->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('users');
}
public function up()
{
Schema::create('job_requests', function (Blueprint $table) {
$table->increments('id');
$table->integer('status')->default(0);
$table->integer('user_id')->unsigned()->nullable();
$table->foreign('user_id')->references('id')->on('users');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('job_requests');
}
I added three more alter migration:
public function up()
{
Schema::table('users', function (Blueprint $table) {
$table->engine = "InnoDB";
});
}
///////////////////////////
public function up()
{
Schema::table('job_requests', function (Blueprint $table) {
$table->engine = "InnoDB";
});
}
///////////////////////////
public function up()
{
Schema::table('applications', function (Blueprint $table) {
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('job_request_id')->references('id')->on('job_requests')->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('applications', function (Blueprint $table) {
$table->dropForeign(['user_id']);
$table->dropForeign(['job_request_id']);
});
}
Without luck yet.
The referenced tables also have to use the InnoDB engine.
You can change them with raw SQL statements:
DB::statement("ALTER TABLE users ENGINE='InnoDB';");
DB::statement("ALTER TABLE job_requests ENGINE='InnoDB';");
for deleting the table:
public function down()
{
Schema::disableForeignKeyConstraints();
Schema::dropIfExists('applications');
}
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?
I get this error while trying to migrate:
SQLSTATE[HY000]: General error: 1005 Can't create table testing.#sql-1848_3ae (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table gallery add constraint gallery_user_id_foreign foreign key (user_id) references users (id))
[PDOException] SQLSTATE[HY000]: General error: 1005 Can't create table testing.#sql-1848_3ae (errno: 150 "Foreign key constraint is incorrectly formed")
here are my migrations:
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->string('password');
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('users');
}
}
class CreateGalleryTable extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::create('gallery', function (Blueprint $table) {
$table->increments('id');
$table->string('description')->nullable();
$table->integer('user_id');
$table->timestamps();
$table->foreign('user_id')->references('id')->on('users');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::drop('gallery');
}
}
I don't believe you can set a forgein key within the Schema::create();
Add this within your up method, but outside of the Schema::create();
Schema::table('gallery', function ($table) {
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
}
Also, consider adding the following to your down() function. I have run into issues trying to reset migrations and not working due to the Foreign Key Constraints.
$table->dropForeign('gallery_user_id_foreign');