Laravel 5.1 foreign key migration error - php

I get this weird error when executing my constraints migration
When I execute this migration I get
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1005 Can't create table 'flowtime_dev.#sql-85bb_52' (errno: 121) (SQL: alter table `questions` add constraint questions_inventory_i
d_foreign foreign key (`inventory_id`) references `inventories` (`id`) on delete cascade)
[PDOException]
SQLSTATE[HY000]: General error: 1005 Can't create table 'flowtime_dev.#sql-85bb_52' (errno: 121)
I've read that the data types in different tables should match ( like int(10) to int(10)) and they do, this is also the last migration that I have ( all the tables exist and have been created by other migrations before).
I seriously have no idea what to do anymore :(. Could someone help?
I have seen similar posts, but they have not fixed the problem so far for me.
The migration is as follows:
public function up()
{
Schema::table('questions', function ($table) {
$table->integer('inventory_id')->unsigned()->change();
$table->foreign('inventory_id')
->references('id')->on('inventories')
->onDelete('cascade');
});
Schema::table('question_response', function ($table) {
$table->integer('question_id')->unsigned()->change();
$table->foreign('question_id')
->references('id')->on('questions')
->onDelete('cascade');
});
Schema::table('question_response', function ($table) {
$table->integer('sample_result_set_id')->unsigned()->change();
$table->foreign('sample_result_set_id')
->references('id')->on('sample_response_set')
->onDelete('cascade');
});
Schema::table('inventory_response', function ($table) {
$table->integer('inventory_id')->unsigned()->change();
$table->foreign('inventory_id')
->references('id')->on('inventories')
->onDelete('cascade');
});
Schema::table('inventory_response', function ($table) {
$table->integer('sample_result_set_id')->unsigned()->change();
$table->foreign('sample_result_set_id')
->references('id')->on('sample_response_set')
->onDelete('cascade');
});
}

In the beginning of your migration turn off foreign key constraints.
SET FOREIGN_KEY_CHECKS=0;
Then, when the data is migrated, turn them back on.
SET FOREIGN_KEY_CHECKS=1;
If that doesn't work, then try adding the following line.
$table->engine = 'InnoDB'
Further, you can rename the constraint since the name may already be used...
$table->foreign('inventory_id', 'fk_questions_inventory_id')->...

Try to use two separate migrations.
Create first migration, which will create tables and all columns (put everything except $table->foreign clauses here). Run migrate command. Do not execute second one, temporarily remove it from migrations directory, if you've already have this migration.
Then create second migration, which will add foreign keys to already created columns (use only $table->foreign clauses here). Run migrate command again.
This helped me, when I was in the same situation. I hope it will work for you too.

Related

SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint laravel 9

Trying to assign foreign key but when you run migrate, I get this this error, I do not understand what the problem is.
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table categories add constraint categories_parent_key_foreign foreign key (parent_key) references categories (key) on delete cascade)
$table->bigIncrements('id');
$table->string('key', 64)->unique();
$table->string('parent_key', 64)->nullable()->index();
$table->string('title', 256)->index()->unique();
$table->foreign('parent_key')->references('key')
->on((new Category())->getConnection()->getDatabaseName() . '.' . Category::TABLE)
->onDelete('cascade');
I had the same problem.
The problem arises when a model has a relationship with itself (self-relation).
To solve this problem, first, the migration file must be created and then the foreign key must be assigned in another migration file.
You must remove the foreign key assignment from the migration file and create the new migration file after that, then add relations statements to assign a foreign key. (the order of the migration files is important).
create_category_table
public function up(): void
{
$table->bigIncrements('id');
$table->string('key', 64)->unique();
$table->string('parent_key', 64)->nullable()->index();
$table->string('title', 256)->index()->unique();
}
create_category_relation_table
public function up(): void
{
$table->foreign('parent_key')->references('key')
->on((new Category())->getConnection()->getDatabaseName() . '.' . Category::TABLE)
->onDelete('cascade');
}
And then php artisan migration
In my case the problem was in the different datatypes of the referenced table key and the key reference. For instance, integer(unsigned) vs bigInteger(unsigned).

Foreign keys error in migrations (Laravel 6.0)

I'm trying to figure out how migrations in Laravel work. Here you can see I'm creating 2 tables. And in the second table (RealNationalLeague), I'm having foreign keys to two columns (real_nation_id, level) in the first table (RealNationalLeagueLevel).
public function up()
{
...
Schema::create(Model\RealNationalLeagueLevel::TABLE, function (Blueprint $table) {
$table->unsignedInteger("real_nation_id");
$table->unsignedTinyInteger("level");
$table->foreign("real_nation_id")->references("id")->on(Model\RealNation::TABLE);
$table->primary(["real_nation_id", "level"]);
});
Schema::create(Model\RealNationalLeague::TABLE, function (Blueprint $table) {
$table->increments("id");
$table->unsignedInteger("real_nation_id");
$table->unsignedTinyInteger("level");
$table->string("name", 32);
$table->foreign("real_nation_id")->references("real_nation_id")->on(Model\RealNationalLeagueLevel::TABLE); // this works
$table->foreign("level")->references("level")->on(Model\RealNationalLeagueLevel::TABLE); // this does not
});
}
Running the migration does not work. It throws QueryException:
SQLSTATE[HY000]: General error: 1005 Can't create table `testdb`.`#sql-17a00_448a37` (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table `real_national_league` add constraint `real_national_league_level_foreign` foreign key (`level`) references `real_national_league_level` (`level`))
Any idea why it's not working? Thanks for any help.
The table that you want to reference, you have to create it first, then in another migration, create a column from this table to that reference table.

Laravel 5.1 - General error: 1005 Can't create table (mysql)

I'm using laravel to migrate some data, but i'm having this message below:
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1005 Can't create table 'heinz.#sql-1e83_8' (errno: 150) (SQL: alter table `funcionarios` add constraint funcionarios_supervisor_id_foreign foreign key (`supervis
or_id`) references `funcionarios` (`id`))
I tried a lot of thing, but didn't work.
Here is the code of the migration file. (the relevant part).
Schema::create('funcionarios', function (Blueprint $table) {
// $table->engine = 'InnoDB';
$table->increments('id');
$table->string('nome', 60);
$table->integer('matricula')->unsigned()->unique();
$table->bigInteger('pis_pasep')->unsigned()->nullable();
$table->date('data_admissao')->nullable();
$table->date('data_demissao')->nullable()->default(null);
$table->date('data_nascimento')->nullable();
$table->string('apelido', 20)->nullable();
$table->integer('supervisor_id')->nullable();
$table->integer('coordenador_id')->nullable();
$table->integer('gerente_id')->nullable();
$table->integer('diretor_id')->nullable();
$table->integer('sexo_id')->nullable();
$table->integer('setor_id')->nullable();
$table->integer('cargo_id');
$table->integer('turno_id')->nullable();
$table->timestamps();
});
Schema::table('funcionarios', function($table){
$table->foreign('supervisor_id')->references('id')->on('funcionarios');
$table->foreign('coordenador_id')->references('id')->on('funcionarios');
$table->foreign('gerente_id')->references('id')->on('funcionarios');
$table->foreign('diretor_id')->references('id')->on('funcionarios');
$table->foreign('sexo_id')->references('id')->on('sexos');
$table->foreign('setor_id')->references('id')->on('setores');
$table->foreign('cargo_id')->references('id')->on('cargos');
$table->foreign('turno_id')->references('id')->on('turnos');
});
All of your foreign keys must be unsigned
$table->integer('supervisor_id')->unsigned()->nullable();
$table->integer('coordenador_id')->unsigned()->nullable();
$table->integer('gerente_id')->unsigned()->nullable();
$table->integer('diretor_id')->unsigned()->nullable();
$table->integer('sexo_id')->unsigned()->nullable();
$table->integer('setor_id')->unsigned()->nullable();
$table->integer('cargo_id')->unsigned();
$table->integer('turno_id')->unsigned()->nullable();
(source: http://laravel.com/docs/4.2/schema#foreign-keys)
Without seeing your table structure, from your below query it could be that
both column id and supervisor_id doesn't match datatype and size. Make sure both datatype and size are same for both this column
Also, check if any other constraint with name funcionarios_supervisor_id_foreign already exists. If so, try providing a different name for the constraint.
alter table `funcionarios` add constraint funcionarios_supervisor_id_foreign
foreign key (`supervisor_id`) references `funcionarios` (`id`)
You get error code 1005 when there is a wrong primary key reference in your code. Here is what you can do to debug your code:
Make sure that your FK you are referring actually exists.
Make sure that you don't have typo. The case must be same too.
FK-linked fields must match definitions exactly.
The execution order of the migration files needs to be checked first. the referenced table migration should be done before refer it in integrity constains.

Access Violation error in Laravel-4 migration

I have migrations as follows:
create_users_table.php
class CreateUsersTable extends Migration {
public function up()
{
Schema::create('users', function(Blueprint $table) {
$table->increments('id');
$table->string('name', 255);
$table->string('username', 64)->unique();
$table->string('email', 255)->unique();
$table->string('password',64);
$table->timestamps();
});
}
public function down()
{
Schema::drop('users');
}
}
create_predictions_table.php
class CreatePredictionsTable extends Migration {
public function up()
{
Schema::create('predictions', function(Blueprint $table) {
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->integer('fixture_id')->unsigned();
$table->foreign('fixture_id')->references('id')->on('fixtures');
$table->integer('home_team_score_prediction');
$table->integer('away_team_score_prediction');
$table->timestamps();
});
}
public function down()
{
Schema::drop('predictions');
}
}
create_fixtures_table.php
class CreateFixturesTable extends Migration {
public function up()
{
Schema::create('fixtures', function(Blueprint $table) {
$table->increments('id');
$table->integer('home_team_id');
$table->integer('away_team_id');
$table->dateTime('date');
$table->string('venue');
$table->timestamps();
});
}
public function down()
{
Schema::drop('fixtures');
}
}
Initially when I ran the migration using
php artisan migrate
I hadn't added foreign key's. I ran
php artisan migrate:refresh
after adding the foreign key constraints which gave me following errror:
SQLSTATE[42S01]: Base table or view already exists: 1050 Table 'predictions' already exists (SQL: create table `predictions` (`id` int unsigned not null auto_increment primary key, `user_id` int unsigned not null, `fixture_id` int
unsigned not null, `home_team_score_prediction` int not null, `away_team_score_prediction` int not null, `created_ at` timestamp default 0 not null, `updated_at` timestamp default 0 not null) default character set utf8 collate utf8_unicode_ci)
I think that is because the table was already there (although refresh is supposed to rollback the migrations and run them again)
When I tried to rollback using
php artisan migrate:rollback
I got the following:
[Illuminate\Database\QueryException]
SQLSTATE[23000]: Integrity constraint violation: 1217 Cannot delete or update a parent row: a foreign key constrain t fails (SQL: drop table `users`)
Where am I going wrong? I'm new to migrations, so these might be errors beginners usually get. But I don't understand what exactly I'm doing wrong. I hope someone can help me out here.
migration will continue until it meets some problem. So probably a foreign key has already been added, but its associated table is not created yet due to error. If you are trying to rollback, migration will assume that the table associated with a foreign key exists and it will try to rollback that table too.
However, that table does not exist!
Well, the simplest way is to empty the database and rerun migration. I met this problem before and find it hard to rollback step by step once such a problem occurs.
What I Did
There is a migrations table for Laravel.
Truncate or empty the table, then delete all tables related to your migrations.
If possible remove any constraints to the foreign keys first before deleting the table.
You should be good to go migrating again.
Addition
Take care of the order of deletion of tables in down() function.
Deleting some tables with referential constraints as declared by onDelete() definitely needs constraints to be removed first.
I think that's causing your rollback error.
Look more into your migrations , buddy :-)

Laravel Migrations self-referencing foreign key General error: 1005 Can't create table

Why do I have a problem creating a table using Laravel Migrations Schema Builder?
The problem occurs with a table with a self-referencing foreign key.
Schema::create('cb_category', function($table)
{
$table->integer('id')->primary()->unique()->unsigned();
$table->integer('domain_id')->unsigned();
$table->foreign('domain_id')->references('id')->on('cb_domain');
$table->integer('parent_id')->nullable();
$table->foreign('parent_id')->references('id')->on('cb_category')->onUpdate('cascade')->onDelete('cascade');
$table->string('name');
$table->integer('level');
});
SQLSTATE[HY000]: General error: 1005 Can't create table 'eklik2.#sql-7d4_e' (errno: 150) (SQL: alter table `cb_category` add constraint cb_category_parent_id_foreign foreign key (`parent_id`) references `cb_category` (`id`) on delete cascade on update cascade) (Bindings: array ())
[PDOException]
SQLSTATE[HY000]: General error: 1005 Can't create table 'eklik2.#sql-7d4_e' (errno: 150)
You have to break this into two Schema blocks, one creating the columns, the other adding the FKs. mysql can't do both at the same time.
Two querys work :
Schema::create('cb_category', function($table)
{
$table->integer('id')->primary()->unique()->unsigned();
$table->integer('parent_id')->nullable();
});
Schema::table('cb_category', function (Blueprint $table)
{
$table->foreign('parent_id')->references('id')->on('cb_category')->onUpdate('cascade')->onDelete('cascade');
});
I may be too late for the party, but the official docs claim that the foreign key, in case of integer, must be ->unsigned();
http://laravel.com/docs/4.2/schema#foreign-keys
Note: When creating a foreign key that references an incrementing
integer, remember to always make the foreign key column unsigned.
Also, Artisan does not fail if you (as I have) misspell unsigned() and I have spent quite a few hours trying to figure out why the key was not created.
So two things:
1. Always make the foreign key column unsigned in case of incrementing integers
2. Check the spelling of unsigned()
Also a late response but probably a more idiomatic way for Laravel 8:
use App\Models\CbCategory;
...
Schema::create("cb_category", function(Blueprint $table)
{
$table->id();
$table->foreignIdFor(CbCategory::class, "parent_id")
->constrained()
->cascadeOnUpdate()
->cascadeOnDelete()
->nullable();
});
Please Note: I guessed the class name of CbCategory here. Using the class reference firsthand (instead of the former table name string) enables your static code checkers to pick up on future class name changes.
Also the _id-suffix at the parent_id column name is important.
May the following resources quench your thirst for knowledge:
id(): https://laravel.com/docs/8.x/migrations#column-method-id
foreignIdFor(): https://laravel.com/api/8.x/Illuminate/Database/Schema/Blueprint.html
cascadeOnDelete() & cascadeOnUpdate(): https://laravel.com/api/8.x/Illuminate/Database/Schema/ForeignKeyDefinition.html
Schema::create('cb_category', function (Blueprint $table) {
$table->increments('id')->unsigned();
$table->integer('domain_id')->unsigned();
$table->foreign('domain_id')->references('id')->on('cb_domain');
$table->integer('parent_id')->nullable();
$table->foreign('parent_id')->references('id')->on('cb_category')->onUpdate('cascade')->onDelete('cascade');
$table->string('name');
$table->integer('level');
});
Try this
I think you have another table that references the current table that you want to create.
I had this problem and remove that table and my problem was solved
Schema::create('categories', function (Blueprint $table) {
$table->id();
$table->integer('parent_id')->unsigned();
$table->foreign('parent_id')->on('categories')->references('id');
});
i got same error when i used this code, after change "$table->integer('parent_id')->unsigned()" to "$table->bigInteger('parent_id');"
my problem solved.
The point here is to make sure that the type of foreign key is the same as the primary key.
Since Laravel 8+ you don't have to break into two Schema blocks. You can use
foreignIdFor(CbCategory::class, 'cb_category_id') and it will create a column named cb_category_id
Ex.
Schema::create("cb_category", function(Blueprint $table)
{
$table->id();
$table->foreignIdFor(CbCategory::class, 'cb_category_id')->nullable()->constrained()->cascadeOnUpdate()->cascadeOnDelete();
});
Any additional column modifiers (Ex. nulleable) must be called before the constrained method.
You can use a second parameter in foreignIdFor for the referencing column name (in case it isn't 'id') NOT for the name you want it to have, in your case the name will automatically be 'cb_category_id'

Categories