Skip to content

fix selecting list of fields and map/2 from subquery - #4777

Merged
greg-rychlewski merged 8 commits into
elixir-ecto:masterfrom
greg-rychlewski:select_from_subquery_fix
Aug 5, 2026
Merged

fix selecting list of fields and map/2 from subquery#4777
greg-rychlewski merged 8 commits into
elixir-ecto:masterfrom
greg-rychlewski:select_from_subquery_fix

Conversation

@greg-rychlewski

Copy link
Copy Markdown
Member

Closes #4776

@greg-rychlewski

Copy link
Copy Markdown
Member Author

ah there is some inspect stuff and old tests i need to fix up

Comment thread test/ecto/query/subquery_test.exs Outdated
assert query.select.fields == [{{:., [type: :string], [{:&, [], [1]}, :title]}, [], []}]

subquery = from p in Post, select: %{id: p.id, title: p.title}
subquery = from p in Post, select: struct(p, [:id, :title])

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@josevalim The fix was surfacing an error in this test. Basically it fails now because select: [atom()] is considered the same as struct/2 more strictly.

Just want to make sure this is still the right interpretation of the list of atoms. It's in the docs but not sure if it's out of date

It is also possible to select a struct and limit the returned fields at the same time:

from(City, select: [:name])

The syntax above is equivalent to:

from(city in City, select: struct(city, [:name]))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, select: [:foo, :bar] keeps whatever the from clause is. Structs if there are source+schema, maps if we only have the source.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah ok I believe what I just pushed now is the correct solution then. sorry for the back and forth. would you mind taking a look once more?

@greg-rychlewski
greg-rychlewski merged commit 6c0c457 into elixir-ecto:master Aug 5, 2026
8 checks passed
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.

select: map(source, fields) and select: [:fields] lose field types when the source is a subquery

2 participants