Skip to content

Ecto.QueryError: when loading aggregates in the query with Ash.Query.load #190

Closed

Description

@diogomrts

Code of Conduct

  • I agree to follow this project's Code of Conduct

AI Policy

  • I agree to follow this project's AI Policy, or I agree that AI was not used while creating this issue.

Versions

  • Elixir: 1.18.4
  • Erlang/OTP: 28.1
  • Ash: 3.9.0
  • AshPostgres: 2.6.25
  • AshSql: 0.3.12
  • Ecto: 3.13.5
  • EctoSql: 3.13.2
  • Postgrex: 0.21.1

Operating system

macOS

Current Behavior

When loading aggregates in a query, Ash generates an Ecto query with select: merge(struct, map) which fails with:

** (Ecto.QueryError) cannot select_merge source "files" into
merge(&0, %{count_contacts_enabled: coalesce(as(1).count_contacts_enabled(), ^0)}),
those select expressions are incompatible. You can only select_merge:

  * a source (such as post) with another source (of the same type)
  * a source (such as post) with a map
  * a struct with a map
  * a map with a map

Incompatible merge found
 in query:

from f0 in File,
  as: 0,
  left_lateral_join: c1 in ^#Ecto.Query<...>,
  as: 1,
  on: true,
  where: ...,
  select: merge(
    struct(f0, [...]),
    %{count_contacts_enabled: coalesce(as(1).count_contacts_enabled, ^0)}
  )

Full stacktrace:

(ecto 3.13.5) lib/ecto/query/builder/select.ex:439: Ecto.Query.Builder.Select.merge/8
(ash_sql 0.3.12) lib/aggregate.ex:2518: AshSql.Aggregate.wrap_in_subquery_for_aggregates/1
(ash_sql 0.3.12) lib/aggregate.ex:112: AshSql.Aggregate.add_aggregates/6
(ash 3.9.0) lib/ash/query/query.ex:4515: Ash.Query.data_layer_query/2
(ash 3.9.0) lib/ash/actions/read/read.ex:789: anonymous fn/9 in Ash.Actions.Read.do_read/5

Reproduction

I've added a commit to this reproduction repository: diogomrts/test_repo@81d0cd5

These two tests should pass but one fails:

mix test test/support/select_merge_bug_test.exs

Expected Behavior

This was working on previous versions and when updating ash from 3.5.43 to a recent version it started raising the error, so was expected to work as before.

Activity

  1. added theissue type on Nov 11, 2025
  2. zachdaniel commented on Nov 15, 2025

    @zachdaniel
    Contributor
  3. zachdaniel commented on Nov 16, 2025

    @zachdaniel
    Contributor

    I believe this is fixed in main

  4. diogomrts commented on Nov 27, 2025

    @diogomrts
    Author

    Hey @zachdaniel i have updated and tested again in my app and the reproduction repo, it now does not have the select_merge issue, but has another issue. The count is not loaded at all, seems like if i try to load 2 things for the same relationship it only loads the first one?

    e.g. if i remove :count_contacts it loads :count_contacts_enabled fine:

    PgFile
    |> Ash.Query.load([:count_contacts_enabled])

    but with both, only loads the count_contacts:

    PgFile
    |> Ash.Query.load([:count_contacts_enabled, :count_contacts])
    1) test demonstrates select_merge bug with aggregate filtering (Helpdesk.Support.SelectMergeBugTest)
         test/support/select_merge_bug_test.exs:29
         Assertion with == failed
         code:  assert hd(files).count_contacts_enabled == 0
         left:  #Ash.NotLoaded<:aggregate, field: :count_contacts_enabled>
         right: 0
         stacktrace:
           test/support/select_merge_bug_test.exs:47: (test)

    should i make another github issue?

  5. zachdaniel commented on Nov 27, 2025

    @zachdaniel
    Contributor

    Have you also updated ash_postgres and ash? Just to make sure?

  6. zachdaniel commented on Nov 27, 2025

    @zachdaniel
    Contributor

    And you're definitely on ash_sql 0.3.14?

  7. diogomrts commented on Nov 27, 2025

    @diogomrts
    Author

    Yes just did a full update for all deps, here is the commit just to e sure nothing is missing 😅
    diogomrts/test_repo@dc13dbd#diff-f9ea769f3d62f6ebfee2c6ef453a579ab051c1b45514ecfc2ffba9d9e98a5025L5-R5

  8. zachdaniel commented on Nov 28, 2025

    @zachdaniel
    Contributor

    @diogomrts fixed in the latest ash_sql

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions