Skip to content

868 bug/resource based inline aggregates failing from differing targets - #889

Merged
zachdaniel merged 9 commits into
ash-project:mainfrom
arstamm:868-bug/resource-based-inline-aggregates-failing-from-differing-targets
Oct 17, 2026
Merged

zachdaniel merged 9 commits into
ash-project:mainfrom
arstamm:868-bug/resource-based-inline-aggregates-failing-from-differing-targets

Conversation

@arstamm

@arstamm arstamm commented Oct 16, 2026 •

Copy link
Copy Markdown
Contributor

Fix For Inline First Aggregate Mismatch

Contributor Checklist

  • [ ✔️ ] I accept the AI Policy, or AI was not used in the creation of this PR.
  • [ ✔️ ] Bug fixes include regression tests

Description

This is a fix to address the resource-based inline aggregates (issue 868) for the posgres_sql repository.

Problem

Whenever a guest schema and table are passed into the first macro, the resulting query attempts to look up the guest table from the local schema, resulting in a mismatch followed by a crash. So far, I've observed this only happening with the first macro only.

The cause is that when the query is generated, the guest table name is kept, but the schema is forgotten completely. This causes the guest table to be auto assigned to the local schema instead, resulting in a crash.

calculate :latest_report, :string, expr(first(Report, field: :title, query: [ filter: expr(author_name == parent(name)), sort: [inserted_at: :desc] ] ))

Solution

The function resource_to_query in lib/query.ex is now configured with the prefix already assigned to the target schema instead of being set to null. This way, the query generated by this starts out correct and does not need to be corrected later down the line. Any functions that need to override this down the line can still do so.

What this PR Includes

This PR are includes the regression test for this bug (test/inline_first_aggregate_schema_test.exs). The test reproduces the code scenario displayed above.

The the bug fix for this test can be found here.

@zachdaniel

Copy link
Copy Markdown
Contributor

👋 thanks! Would you mind finding a good test for this among the existing test files? There should be like an "aggregates test". Thanks 🙇

@arstamm

arstamm commented Oct 16, 2026

Copy link
Copy Markdown
Contributor Author

Hi, Zach! Just double checking, you need a test in ash_sql as well that checks for aggregations. Is that correct? Also, would you like that as a new pull request, or just add it to the same branch as this one?

@zachdaniel

Copy link
Copy Markdown
Contributor

Nope, just a test in ash_postgres

@arstamm

arstamm commented Oct 16, 2026

Copy link
Copy Markdown
Contributor Author

This PR already contains a test for the specified bug. Would you like an additional test in an additional file for this same bug as well? Just want to make sure we are on the same page.

@zachdaniel

Copy link
Copy Markdown
Contributor

Sorry, what I meant was that this PR contains a test in its own file, but in general we prefer to keep tests organized into fewer files. So if you could find a good other file to put this test in that would be great :)

@arstamm

arstamm commented Oct 17, 2026

Copy link
Copy Markdown
Contributor Author

Same page status acquired! The regression test now lives in test/aggregate_test.exs. Feel free to let me know if you'd like anything else changed.

@zachdaniel
zachdaniel merged commit dbdbea9 into ash-project:main Oct 17, 2026
121 of 126 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