mirror of
https://github.com/ash-project/ash_postgres.git
synced 2024-09-19 13:03:14 +12:00
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:
parent
c47ed40a55
commit
8f79fcb59d
1 changed files with 35 additions and 1 deletions
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue