Skip to content

Conversation

Gigitsu
Copy link
Contributor

@Gigitsu Gigitsu commented Jun 23, 2025

Fixes a regression that prevents some queries from compiling correctly.

Current behavior

Given the following setup:

defmodule MyQueryUtils do
  defmacro foo(table) do
    quote(do: unquote(table).priority > 0)
  end
end

defmodule Post do
  use Ecto.Schema

  @primary_key false
  schema "posts" do
    field :title, :string
    field :priority, :integer
  end
end

This query no longer compiles:

import MyQueryUtils
import Ecto.Query

select(Post, [o], %{row_number: over(row_number(), order_by: [desc: foo(o)])})

With the following error:

** (KeyError) key :module not found in: {#Macro.Env<
   aliases: [],
   ...
 >, #Function<4.25497058/5 in Ecto.Query.Builder.Select.escape_expansion>}

If you are using the dot syntax, such as map.field, make sure the left-hand side of the dot is a map
    (elixir 1.18.3) lib/macro/env.ex:539: Macro.Env.expand_import/5
    (elixir 1.18.3) lib/macro.ex:1877: Macro.do_expand_once/2
    (elixir 1.18.3) lib/macro.ex:1824: Macro.expand_once/2
    (ecto 3.13.1) lib/ecto/query/builder.ex:1101: Ecto.Query.Builder.try_expansion/5
    (ecto 3.13.1) lib/ecto/query/builder/order_by.ex:79: Ecto.Query.Builder.OrderBy.do_escape/5
    (elixir 1.18.3) lib/enum.ex:1316: anonymous fn/3 in Enum.flat_map_reduce/3
    (elixir 1.18.3) lib/enum.ex:4968: Enumerable.List.reduce/3
    (elixir 1.18.3) lib/enum.ex:1315: Enum.flat_map_reduce/3

@greg-rychlewski
Copy link
Member

One tiny comment then it's good to go! Thank you

@greg-rychlewski greg-rychlewski merged commit 5750491 into elixir-ecto:master Jun 23, 2025
7 checks passed
@greg-rychlewski
Copy link
Member

Thanks very much for the fix!

@Gigitsu
Copy link
Contributor Author

Gigitsu commented Jun 23, 2025

Great! Thank you

@Gigitsu Gigitsu deleted the fix/order_by_in_over branch June 24, 2025 10:54
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