mirror of
https://github.com/ash-project/ash.git
synced 2024-09-19 21:13:10 +12:00
Handle relationship filter on primary key (#1254)
Co-authored-by: Andreas Donig <git@innwiese.de>
This commit is contained in:
parent
7f2e7d3ec1
commit
23d504e151
1 changed files with 32 additions and 22 deletions
|
@ -2714,29 +2714,39 @@ 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
|
||||
case parse_expression(nested_statement, context) do
|
||||
{:ok, nested_expression} ->
|
||||
{:ok, BooleanExpression.optimized_new(:and, expression, nested_expression)}
|
||||
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)}
|
||||
|
||||
{:error, error} ->
|
||||
{:error, error}
|
||||
end
|
||||
else
|
||||
with [field] <- Ash.Resource.Info.primary_key(context.resource),
|
||||
attribute <- attribute(context, field),
|
||||
{:ok, casted} <-
|
||||
Ash.Type.cast_input(attribute.type, nested_statement, attribute.constraints) do
|
||||
add_expression_part({field, casted}, context, expression)
|
||||
else
|
||||
_other ->
|
||||
{:error,
|
||||
InvalidFilterValue.exception(
|
||||
value: inspect(nested_statement),
|
||||
message:
|
||||
"a single value must be castable to the primary key of the resource: #{inspect(context.resource)}"
|
||||
)}
|
||||
end
|
||||
{:error, error} ->
|
||||
{:error, error}
|
||||
end
|
||||
|
||||
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 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)
|
||||
else
|
||||
_other ->
|
||||
{:error,
|
||||
InvalidFilterValue.exception(
|
||||
value: inspect(nested_statement),
|
||||
message:
|
||||
"a single value must be castable to the primary key of the resource: #{inspect(context.resource)}"
|
||||
)}
|
||||
end
|
||||
end
|
||||
|
||||
attr = attribute(context, field) ->
|
||||
|
|
Loading…
Reference in a new issue