diff --git a/lib/expr.ex b/lib/expr.ex index 4f86c90..f30ec3b 100644 --- a/lib/expr.ex +++ b/lib/expr.ex @@ -3523,6 +3523,8 @@ defmodule AshSql.Expr do :bracket end + declared_field = type && field_constraints(constraints, next) + {next, type, constraints} = cond do type && Ash.Type.embedded_type?(type) -> @@ -3559,6 +3561,10 @@ defmodule AshSql.Expr do {name, type, constraints} end + declared_field -> + {field_type, field_constraints} = declared_field + {next, field_type, field_constraints} + true -> {next, nil, nil} end @@ -3602,6 +3608,20 @@ defmodule AshSql.Expr do end end + # The type a `fields` constraint declares for `next`, if it declares one. + defp field_constraints(constraints, next) do + with true <- Keyword.keyword?(constraints || []), + fields when is_list(fields) <- constraints[:fields], + {_name, config} <- + Enum.find(fields, fn {name, _config} -> + if is_binary(next), do: to_string(name) == next, else: name == next + end) do + {config[:type], config[:constraints] || []} + else + _ -> nil + end + end + defp list_requires_encoding?(value) do !Enum.empty?(value) && Enum.any?(value, fn value ->