Nullify product brand_id when a brand is deleted#2472
Merged
Conversation
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
alecritson
approved these changes
May 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Brand::deletingnow also setsbrand_id = nullon every product (active and soft-deleted) belonging to the brand before the brand row is removed.brand_idcleared.Why
The brand delete hook was detaching the
brand_discountandbrand_collectionpivots but doing nothing with thelunar_products.brand_idFK. On databases that actually enforce FKs the brand delete then failed with: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. Clearingbrand_idon both active and trashed products lets the delete go through cleanly.brand_idis already nullable on the products table, so there is no schema change.Fixes #2030.
Test plan
vendor/bin/pest --testsuite=core— 503 passing