fix: resolve actor/context templates in through relationship filters#789
Merged
zachdaniel merged 1 commit intoJul 7, 2026
Conversation
Relationship filters used by `has_many ... through` relationships that
reference `^actor/1`, `^context/1`, `^arg/1` or `^tenant/0` templates were
applied without filling the template, so the raw template (e.g.
`{:_context, :sample_context}`) was handed to the SQL implementation and
raised "Unsupported expression in ... query".
Fill the templates before compiling the filter into SQL, mirroring how a
directly loaded relationship filter is hydrated: actor/args/tenant come
from the source query while the context is taken from the query the filter
is being applied to.
Fixes ash-project#788
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Contributor
|
🚀 Thank you for your contribution! 🚀 |
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.
Problem
Fixes #788.
When a
has_manyrelationship carries a filter that uses a^context/1or^actor/1template, and that relationship is used inside ahas_many ... throughrelationship, loading the through relationship fails:The intermediate relationship's filter was applied via
Ash.Query.do_filterwithout filling the template, so the raw template ({:_context, :sample_context}) was handed to the SQL implementation, which cannot compile it. The same class of bug affected^actor/1,^arg/1and^tenant/0templates.Fix
Fill
^actor/^context/^arg/^tenanttemplates before the intermediate relationship filter is compiled into SQL, in both through code paths inlib/data_layer.ex(the recursivebuild_through_queries/4used for 3+ hop paths, and the inline path used for 2-hop /many_to_many).This mirrors how a directly loaded relationship filter is hydrated in
Ash.Actions.Read.Relationships: actor/args/tenant come from the source query, while the context is taken from the query the filter is being applied to (after itsset_context). As a result, loading athroughrelationship resolves templates identically to loading each hop directly.Tests
Added intermediate relationships with
^contextand^actorfilters on theThrough.Schooltest resource, plusthroughrelationships using them, covering both the 3-hop (build_through_queries) and 2-hop (inline) paths. New tests intest/through_relationships_test.exsassert:^context/^actortemplates no longer raise, and^actortemplate resolves to the actor's value and filters the through relationship correctly.Full suite: 832 passing,
mix formatclean,mix ash_postgres.generate_migrations --checkclean.🤖 Generated with Claude Code