Skip to content

Nullify product brand_id when a brand is deleted#2472

Merged
glennjacobs merged 1 commit into
1.xfrom
fix/brand-delete-fk-constraint-2030
May 19, 2026
Merged

Nullify product brand_id when a brand is deleted#2472
glennjacobs merged 1 commit into
1.xfrom
fix/brand-delete-fk-constraint-2030

Conversation

@glennjacobs
Copy link
Copy Markdown
Contributor

Summary

  • Brand::deleting now also sets brand_id = null on every product (active and soft-deleted) belonging to the brand before the brand row is removed.
  • Extends the existing "can delete a brand" test to assert both an active and a soft-deleted product have their brand_id cleared.

Why

The brand delete hook was detaching the brand_discount and brand_collection pivots but doing nothing with the lunar_products.brand_id FK. On databases that actually enforce FKs the brand delete then failed with:

SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (.lunar_products, CONSTRAINT lunar_products_brand_id_foreign …)

The reporter's repro removes all active products first but the FK still fires because Product uses SoftDeletes — the rows are still there with the brand reference. Clearing brand_id on both active and trashed products lets the delete go through cleanly. brand_id is already nullable on the products table, so there is no schema change.

Fixes #2030.

Test plan

  • vendor/bin/pest --testsuite=core — 503 passing
  • Manual on MySQL/MariaDB: create brand → assign product → soft-delete product → delete brand from admin (no FK error)

Brand::deleting only detached discounts and collections, leaving any
products still pointing at the brand. The lunar_products.brand_id FK
then prevented the delete, including for soft-deleted products whose
rows still hold the reference.

brand_id on products is already nullable, so clearing it (including
on trashed products) is the right cleanup — brand membership is
metadata, not core to the product.

Fixes #2030
@glennjacobs glennjacobs merged commit e155dcf into 1.x May 19, 2026
16 checks passed
@glennjacobs glennjacobs deleted the fix/brand-delete-fk-constraint-2030 branch May 19, 2026 08:55
@github-project-automation github-project-automation Bot moved this from Todo to Done in Roadmap May 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Unable to delete a brand

2 participants