migration error

i create new migration to add cascadeondelete() but when i try to run migrations i have this error SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'purchases_company_id_foreign'; check that column/key exists (Connection: mysql, SQL: alter table purchases drop foreign key purchases_company_id_foreign)
9 Replies
gigiloouu
gigiloouuOP9mo ago
there is what i wrote in my migration Schema::table('purchases', function (Blueprint $table) { $table->dropForeign(['company_id']); $table->dropForeign(['supplier_id']); $table->dropForeign(['subbrand_id']); $table->dropForeign(['currency_id']); $table->foreign('company_id') ->references('id') ->on('purchases') ->cascadeOnDelete(); $table->foreign('supplier_id') ->references('id') ->on('purchases') ->cascadeOnDelete(); $table->foreign('subbrand_id') ->references('id') ->on('purchases') ->cascadeOnDelete(); $table->foreign('currency_id') ->references('id') ->on('purchases') ->cascadeOnDelete(); }); anyonee?
toeknee
toeknee9mo ago
This isn't a filament issue and please don't expect a prompt response without paying for someone, but a laravel issue. check the key exists first or try catch.
Schema::table('purchases', function (Blueprint $table) {
try {
$table->dropForeign(['company_id']);
} catch (Exception $e) {
// Log here
}
$table->dropForeign(['supplier_id']);
$table->dropForeign(['subbrand_id']);
$table->dropForeign(['currency_id']);

$table->foreign('company_id')
->references('id')
->on('purchases')
->cascadeOnDelete();
$table->foreign('supplier_id')
->references('id')
->on('purchases')
->cascadeOnDelete();
$table->foreign('subbrand_id')
->references('id')
->on('purchases')
->cascadeOnDelete();
$table->foreign('currency_id')
->references('id')
->on('purchases')
->cascadeOnDelete();
});`
Schema::table('purchases', function (Blueprint $table) {
try {
$table->dropForeign(['company_id']);
} catch (Exception $e) {
// Log here
}
$table->dropForeign(['supplier_id']);
$table->dropForeign(['subbrand_id']);
$table->dropForeign(['currency_id']);

$table->foreign('company_id')
->references('id')
->on('purchases')
->cascadeOnDelete();
$table->foreign('supplier_id')
->references('id')
->on('purchases')
->cascadeOnDelete();
$table->foreign('subbrand_id')
->references('id')
->on('purchases')
->cascadeOnDelete();
$table->foreign('currency_id')
->references('id')
->on('purchases')
->cascadeOnDelete();
});`
for each foreign
gigiloouu
gigiloouuOP9mo ago
yep i know and thanks for ur time what i should wirte in catch? like what menas Log here there is my table btw which i want to update
Schema::create('purchases', function (Blueprint $table) {
$table->id();
$table->string('code')->nullable();
$table->foreignIdFor(Company::class)->constrained();
$table->foreignIdFor(Supplier::class)->constrained();
$table->foreignIdFor(Subbrand::class)->nullable()->constrained();
$table->foreignIdFor(Currency::class)->constrained();
$table->integer('transporter_amount')->nullable();
$table->integer('transporter_currency_id')->nullable();
$table->float('sum_amount');
$table->date('order_date')->nullable();
$table->date('delivery_date')->nullable();
$table->foreignIdFor(Transporter::class)->nullable()->constrained();
$table->integer('status')->default(0)->comment('0-მიმდინარე, 1-შესაკვეთი, 2-დასრულებული, 3-გაუქმებული');
$table->boolean('pre_pay')->default(false)->comment('1-წინასწარი გადახდით, 0-სტანდარტული');
$table->timestamps();
Schema::create('purchases', function (Blueprint $table) {
$table->id();
$table->string('code')->nullable();
$table->foreignIdFor(Company::class)->constrained();
$table->foreignIdFor(Supplier::class)->constrained();
$table->foreignIdFor(Subbrand::class)->nullable()->constrained();
$table->foreignIdFor(Currency::class)->constrained();
$table->integer('transporter_amount')->nullable();
$table->integer('transporter_currency_id')->nullable();
$table->float('sum_amount');
$table->date('order_date')->nullable();
$table->date('delivery_date')->nullable();
$table->foreignIdFor(Transporter::class)->nullable()->constrained();
$table->integer('status')->default(0)->comment('0-მიმდინარე, 1-შესაკვეთი, 2-დასრულებული, 3-გაუქმებული');
$table->boolean('pre_pay')->default(false)->comment('1-წინასწარი გადახდით, 0-სტანდარტული');
$table->timestamps();
toeknee
toeknee9mo ago
It means log here if you want to, else it catches it and lets the rest of the code run. It's basic php. If you are unsure, I advise you google and research how to use php try catch.
gigiloouu
gigiloouuOP9mo ago
Like i have problem that i cant delete rows on my table in my filament app becouse i have foreignkey error so i should change this 4 column right? please anyone help me how i can delete foregin id items ? im trying to solve this about 2 days 😄
toeknee
toeknee9mo ago
I have told you above.
gigiloouu
gigiloouuOP9mo ago
its not working.. aa wait i found a solution i think 😄 SQLSTATE[42000]: Syntax error or access violation: 1091 Can't DROP 'purchase_brand_purchase_id_foreign'; check that column/key exists (Connection: mysql, SQL: alter table purchase_brand drop foreign key purchase_brand_purchase_id_foreign)
gigiloouu
gigiloouuOP9mo ago
No description
gigiloouu
gigiloouuOP9mo ago
but its exist 5 min ago second table was same error and now its run but now i have same problem in this table ah thats works but migrations run when i refresh migrations is it possible tu run this migrations without that? becouse i dont want to lose my data tf its deleted but when i try to add new i have this error now.. SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (biblusi_purchase.purchases, CONSTRAINT purchases_company_id_foreign FOREIGN KEY (company_id) REFERENCES purchases (id) ON DELETE CASCADE) tf why? ?/
Want results from more Discord servers?
Add your server