Handle relationship filter on primary key (#1254)

Co-authored-by: Andreas Donig <git@innwiese.de>
This commit is contained in:
Andreas Donig 2024-06-24 00:20:28 +02:00 committed by GitHub
parent 7f2e7d3ec1
commit 23d504e151
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -2714,7 +2714,8 @@ defmodule Ash.Filter do
|> Map.update!(:relationship_path, fn path -> path ++ [rel.name] end)
|> Map.put(:resource, rel.destination)
if is_list(nested_statement) || is_map(nested_statement) do
cond do
is_list(nested_statement) || is_map(nested_statement) ->
case parse_expression(nested_statement, context) do
{:ok, nested_expression} ->
{:ok, BooleanExpression.optimized_new(:and, expression, nested_expression)}
@ -2722,9 +2723,18 @@ defmodule Ash.Filter do
{:error, error} ->
{:error, error}
end
else
rel.type != :many_to_many && !Map.get(rel, :no_attributes) &&
[rel.source_attribute] == Ash.Resource.Info.primary_key(rel.destination) ->
with attr <- attribute(%{public?: true, resource: rel.source}, rel.source_attribute),
%Ash.Resource.Attribute{type: type, constraints: constraints} = attr,
{:ok, casted} <- Ash.Type.cast_input(type, nested_statement, constraints) do
add_expression_part({attr, casted}, %{context | resource: rel.source}, expression)
end
true ->
with [field] <- Ash.Resource.Info.primary_key(context.resource),
attribute <- attribute(context, field),
attribute when not is_nil(attribute) <- attribute(context, field),
{:ok, casted} <-
Ash.Type.cast_input(attribute.type, nested_statement, attribute.constraints) do
add_expression_part({field, casted}, context, expression)