Skip to content

fix: recompute changed? after setup_managed_belongs_to_relationships#2647

Merged
zachdaniel merged 1 commit intoash-project:mainfrom
barnabasJ:fix/manage-relationship-hook-changed-flag
Mar 24, 2026
Merged

fix: recompute changed? after setup_managed_belongs_to_relationships#2647
zachdaniel merged 1 commit intoash-project:mainfrom
barnabasJ:fix/manage-relationship-hook-changed-flag

Conversation

@barnabasJ
Copy link
Copy Markdown
Contributor

Summary

  • When manage_relationship is called from before_transaction or before_action hooks on update actions, the changed? flag was computed before setup_managed_belongs_to_relationships runs, so it remained false even after the FK was set via force_change_attribute
  • This caused the cond branch to skip the DB update entirely — the related record was created but the source record's FK was never persisted
  • Fix: recompute changed? after setup_managed_belongs_to_relationships returns, so FK changes from hook-added managed relationships are detected

Companion PR: ash-project/ash_postgres — regression tests (will link once created)

Test plan

  • Added test in test/manage_relationship_test.exs for update + before_transaction hook with ETS data layer
  • All 33 manage_relationship tests pass
  • Full ash_postgres suite passes (713 tests) with this fix

When manage_relationship is called from before_transaction or
before_action hooks on update actions, the FK attribute gets set via
force_change_attribute inside setup_managed_belongs_to_relationships.
However, the changed? flag was computed before this point, so it
remained false, causing the DB update to be skipped entirely. The
related record was created but the source record's FK was never
persisted.

Recompute changed? after setup_managed_belongs_to_relationships returns
so that FK changes from managed relationships are detected.
@barnabasJ
Copy link
Copy Markdown
Contributor Author

Companion regression tests: ash-project/ash_postgres#727

@zachdaniel zachdaniel merged commit d526c74 into ash-project:main Mar 24, 2026
38 of 45 checks passed
@zachdaniel
Copy link
Copy Markdown
Contributor

🚀 Thank you for your contribution! 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants