fix: allow non-unique has_many source_attributes (#355)

* fix: allow non-unique has_many source_attributes

* fix composite primary key case
This commit is contained in:
Jesse Williams 2024-07-18 13:37:31 -07:00 committed by GitHub
parent c47ed40a55
commit 8f79fcb59d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1044,9 +1044,43 @@ defmodule AshPostgres.DataLayer do
{:ok, data_layer_query} ->
source_values = Enum.map(root_data, &Map.get(&1, source_attribute))
source_filter =
case source_pkey do
[] ->
Ecto.Query.dynamic([source], field(source, ^source_attribute) in ^source_values)
[field] ->
values = Enum.map(root_data, &Map.get(&1, field))
Ecto.Query.dynamic([source], field(source, ^field) in ^values)
fields ->
Enum.reduce(root_data, nil, fn record, acc ->
row_match =
Enum.reduce(fields, nil, fn field, acc ->
if is_nil(acc) do
Ecto.Query.dynamic(
[source],
field(source, ^field) == ^Map.get(record, field)
)
else
Ecto.Query.dynamic(
[source],
field(source, ^field) == ^Map.get(record, field) and ^acc
)
end
end)
if is_nil(acc) do
row_match
else
Ecto.Query.dynamic(^row_match or ^acc)
end
end)
end
data_layer_query =
from(source in data_layer_query,
where: field(source, ^source_attribute) in ^source_values
where: ^source_filter
)
if query.__ash_bindings__[:__order__?] do