I'm about to refresh my migration. But php artisan migrate:refresh command is not working.
It shows the following error:
[Illuminate\Database\QueryException] SQLSTATE[42000]: Syntax error
or access violation: 1091 Can't DROP 'classes_userid_foreign'; check
that column/key exists (SQL: alter table classes drop foreign key
classes_userid_foreign)
[PDOException] SQLSTATE[42000]: Syntax error or access violation:
1091 Can't DROP 'classes_userid_foreign'; check that column/key
exists
I even used the dropForeign but its not working for me.
I'm showing my migration file
public function up()
{
Schema::create('classes', function (Blueprint $table) {
$table->increments('id');
$table->integer('userId')->unsigned();
$table->string('title');
$table->string('name');
$table->string('code')->unique();
$table->integer('capacity')->unsigned();
$table->integer('tagId')->unsigned();
$table->foreign('userId')->references('id')->on('users');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('classes', function (Blueprint $table) {
$table->dropForeign(['userId']);
});
Schema::drop('classes');
}
How to fix this problem ?
Most times when you run migrations in laravel (whether up or down), and an error occurs along the line, the transaction may have completed partially without registering it in the migrations table.
If this is the case, or you will like to drop tables whether or not there really exists a foreign key, then you should consider disabling foreign key checks like so:
public function down()
{
Schema::disableForeignKeyConstraints();
Schema::drop('classes');
Schema::enableForeignKeyConstraints();
}
Related
public function up()
{
Schema::create('company_eligibilities', function (Blueprint $table) {
$table->id();
$table->string('marks');
$table->integer('company_id')->unsigned();
$table->timestamps();
$table->foreign('company_id')
->references('id')->on('companies')
->onDelete('cascade')
->onUpdate('cascade');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::dropIfExists('company_eligibilities');
}
Company migration already did before executing the above migration
I am getting the above error when I am trying to create a table along with the foreign key. Have I done something wrong ??
Make sure that, you migrate companies before migrate company_eligibilities, change your foreign key id to
unsigned big integer, Laravel 5.8 make default foreign key as unsigned big integer, also for 6.x and 7.x :
$table->bigInteger('company_id')->unsigned();
Or,
$table->unsignedBigInteger('company_id');
Hi there is have looked for solutions on Stack an couldn't solve my problem.
I am Using Laravel 7.x. At first my migrations where working properly but when i changer$table->foreignId("exp_id")->constrained("expenses")->index(); to $table->foreignId("fk_expense")->constrained("expenses")->index();, it started to show this error on migration
SQLSTATE[HY000]: General error: 1005 Can't create table `edmart`.`cancelled_ex
ps` (errno: 121 "Duplicate key on write or update") (SQL: alter table `cancelled
_exps` add constraint `1` foreign key (`fk_expense`) references `expenses` (`id`
))
Bellow are the migration file for expenses
public function up(){
Schema::create('expenses', function (Blueprint $table) {
$table->id();
$table->foreignId("user_id")->constrained("users")->index();
$table->string("desc");
$table->string("amount");
$table->string("status");
$table->timestamps();
});
}
cancelled expenses migration files
public function up()
{
Schema::create('cancelled_exps', function (Blueprint $table) {
$table->id();
$table->foreignId("fk_expense")->constrained("expenses")->index();
$table->string('viewed');
$table->timestamps();
});
}
I tried to remove the database and create a new one but still it failed.
Update:
All other migrations work properly except cancelled_exps
i changed the $table->foreignId("fk_expense")->constrained("expenses")->index() to
$table->foreignId("expences_id")->constrained()
$table->index('expense_id');
I changed the foreign key column name to expense_id hence i don't
have to specify the expenses table in constrained() chained
method.
I also changed the way i was chaining the index.
The whole migration file looks like this....
public function up()
{
Schema::create('cancelled_exps', function (Blueprint $table) {
$table->id();
$table->foreignId("expense_id")->constrained();
$table->index("expense_id");
$table->string('viewed');
$table->timestamps();
});
}
This question already has an answer here:
Drop muli-column unique key without dropping foreign key?
(1 answer)
Closed 4 years ago.
I'm trying to drop a unique constraint and I keep running into a foreign key constraint issue:
Here's the original migration:
Schema::table('table', function (Blueprint $table) {
$table->bigInteger('other_table_id')->unsigned()->unique()->nullable();
$table->foreign('other_table_id')->references('id')->on('other_table')->onDelete('set null');
});
Fast forward to now, I'm trying to drop the UNIQUE constraint from the column on the table without affecting the foreign key.
Schema::table('table', function (Blueprint $table) {
$table->dropUnique('table_other_table_id_unique');
});
Here's the error I'm getting.
SQLSTATE[HY000]: General error: 1553 Cannot drop index 'table_other_table_id_unique': needed in a foreign key constraint
I've also tried using
Schema::table('table', function (Blueprint $table) {
$table->dropUnique(['other_table_id']);
});
But that didn't work either.
I even tried disabling the foreign key constraint while doing the migration:
Schema::disableForeignKeyConstraints(); // Disable constraint
Schema::table('table', function (Blueprint $table) {
$table->dropUnique(['other_table_id']);
});
Schema::enableForeignKeyConstraints(); // Reenable constraint
Nothing seems to work. What am I doing wrong?
The link Joel Hinz had commented was enough information to answer my question. I'm answering my own question since I can provide the answer with code specific to Laravel.
The issue stemmed from the foreign key using the unique key as its key. Therefore, the foreign key must be dropped before dropping the unique key, then set up the foreign key again.
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('table', function (Blueprint $table) {
$table->dropForeign(['other_table_id']);
$table->dropUnique(['other_table_id']);
$table->foreign('other_table_id')->references('id')->on('other_table')->onDelete('set null');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('table', function (Blueprint $table) {
$table->unique('other_table_id');
});
}
I'm trying to rollback my database, but have this error:
[Illuminate\Database\QueryException]
SQLSTATE[23000]: Integrity constraint violation: 1217 Cannot delete or update a parent row: a foreign key constraint fails (SQL: drop table tb_levels)
[PDOException]
SQLSTATE[23000]: Integrity constraint violation: 1217 Cannot delete or update a parent row: a foreign key constraint fails
this is my migration code:
public function up()
{
Schema::disableForeignKeyConstraints();
Schema::create('tb_users', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->increments('id_user');
$table->string('name');
$table->string('username');
$table->string('email')->unique();
$table->integer('id_level')->unsigned();
$table->string('password', 60);
$table->rememberToken();
$table->boolean('activated')->default(false);
$table->timestamps();
$table->foreign('id_level')->references('id_level')->on('tb_levels');
});
Schema::enableForeignKeyConstraints();
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::disableForeignKeyConstraints();
Schema::table('tb_users', function(Blueprint $table){
$table->dropForeign('tb_users_id_level_foreign');
$table->dropColumn('id_level');
});
Schema::drop('tb_users');
Schema::enableForeignKeyConstraints();
}
I've tried several ways that I found in this forum, but still got that error, any help please?
First Disable Foreign Key using this:
SET FOREIGN_KEY_CHECKS=1;
SET GLOBAL FOREIGN_KEY_CHECKS=1;
And then migrate your database.
Again Apply Foreign key constrain:
SET FOREIGN_KEY_CHECKS=0;
SET GLOBAL FOREIGN_KEY_CHECKS=0;
Well, finally I found a way to solve that error,
First of all you need to make a migration to drop the foreign key and column, this is the code:
public function up()
{
Schema::disableForeignKeyConstraints();
Schema::table('tb_users', function(Blueprint $table){
$table->dropForeign('tb_users_id_level_foreign');
$table->dropColumn('id_level');
});
Schema::enableForeignKeyConstraints();
}
Then migrate it, after that It will drop the column and foreign key.
After that delete Schema::table code, save it, and run command:
php artisan migrate:reset
Well, It works but it's really an impractical way,
Hope that out there had more easy way rather than this.
I have 3 models: User , ChangeMoney and CurrencyType, I have simple relationship with User and ChangeMoney and I want to add another relationship with ChangeMoney table with CurrencyType, but I get this error:
D:\xampp\htdocs\epay-pro>php artisan migrate
[Illuminate\Database\QueryException]
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL
: alter table `change_money` add constraint change_money_currency_id_foreig
n foreign key (`currency_id`) references `currency_type` (`id`))
current my migration is:
Schema::create('change_money',function(Blueprint $table){
$table->increments('id');
$table->tinyInteger('currency_type');
$table->string('current_money');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->integer('currency');
$table->timestamps();
});
and I don't have any problem, now I want to add other foreign key to CurrencyType table such as:
Schema::create('change_money',function(Blueprint $table){
$table->increments('id');
$table->tinyInteger('currency_type');
$table->string('current_money');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->integer('currency_id')->unsigned();
$table->foreign('currency_id')->references('id')->on('currency_type');
$table->timestamps();
});
currency_type table migration:
Schema::create('currency_type',function(Blueprint $table){
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->string('currency_type','50');
$table->char('currency_symbol','1');
$table->timestamps();
});
Migrations in Laravel runs in order. I don't now your migrations order, but I think that your change_money migration is running first than your currency_type migration. So when the aplication try to add a foreign key in ChangeMoney table to CurrencyType table, the CurrencyType table not exist yet.
Try to create a new migration to add just the relationship between ChangeMoney table and CurrencyType table
You have to remove data from both tables ('change_money', 'currency_type').
Or you can turn off the check of foreign keys. In MySQL, or in the phpmyadmin run this:
SET FOREIGN_KEY_CHECKS=0;
Run migration in Laravel: php artisan migrate
And then again turn on check of foreign keys:
SET FOREIGN_KEY_CHECKS=1;
PROBLEM SOLVED by split foreign key to other migration
class ForeignKeyBetweenChangeMoneyAndCurrencyType extends Migration
{
/**
* Run the migrations.
*
* #return void
*/
public function up()
{
Schema::table('change_money', function (Blueprint $table) {
$table->integer('currency_id')->unsigned();
$table->foreign('currency_id')->references('id')->on('currency_type');
});
}
/**
* Reverse the migrations.
*
* #return void
*/
public function down()
{
Schema::table('change_money', function(Blueprint $table) {
$table->dropForeign(' change_money_currency_id_foreign');
});
}
}