fix: cast calcs in query expressions

This commit is contained in:
Zach Daniel 2022-05-20 13:42:20 -04:00
parent 4ec454d4f1
commit d2e5c71a6b
3 changed files with 10 additions and 6 deletions

View file

@ -442,9 +442,10 @@ defmodule AshPostgres.Expr do
}, },
bindings, bindings,
embedded?, embedded?,
type _type
) do ) do
calculation = %{calculation | load: calculation.name} calculation = %{calculation | load: calculation.name}
type = AshPostgres.Types.parameterized_type(calculation.type, [])
case Ash.Filter.hydrate_refs( case Ash.Filter.hydrate_refs(
calculation.module.expression(calculation.opts, calculation.context), calculation.module.expression(calculation.opts, calculation.context),
@ -519,7 +520,7 @@ defmodule AshPostgres.Expr do
} = ref, } = ref,
bindings, bindings,
embedded?, embedded?,
type _type
) do ) do
binding_to_replace = binding_to_replace =
Enum.find_value(bindings.bindings, fn {i, binding} -> Enum.find_value(bindings.bindings, fn {i, binding} ->
@ -533,6 +534,8 @@ defmodule AshPostgres.Expr do
|> Map.delete(0) |> Map.delete(0)
|> Map.update!(binding_to_replace, &Map.merge(&1, %{path: [], type: :root})) |> Map.update!(binding_to_replace, &Map.merge(&1, %{path: [], type: :root}))
type = AshPostgres.Types.parameterized_type(calculation.type, [])
case Ash.Filter.hydrate_refs( case Ash.Filter.hydrate_refs(
calculation.module.expression(calculation.opts, calculation.context), calculation.module.expression(calculation.opts, calculation.context),
%{ %{

View file

@ -248,6 +248,7 @@ defmodule AshPostgres.CalculationTest do
test "calculations using if and literal boolean results can run" do test "calculations using if and literal boolean results can run" do
Post Post
|> Ash.Query.load(:was_created_in_the_last_month) |> Ash.Query.load(:was_created_in_the_last_month)
|> Ash.Query.filter(was_created_in_the_last_month == true)
|> Api.read!() |> Api.read!()
end end

View file

@ -99,11 +99,11 @@ defmodule AshPostgres.Test.Post do
:boolean, :boolean,
expr( expr(
# This is written in a silly way on purpose, to test a regression # This is written in a silly way on purpose, to test a regression
if fragment("(? <= (now() - '1 month'::interval))", created_at) do if(
true fragment("(? <= (now() - '1 month'::interval))", created_at),
else true,
false false
end )
) )
) )
end end