vkryukov

vkryukov

Can Ash calculation return a map?

Hi, I have the following calculation in one of my resources:

  calculations do
    calculate :llm_model,
              :map,
              expr(
                cond do
                  role == :basic -> %{"model" => "gpt-4o-mini"}
                  role == :pro -> %{"model" => "o3-mini", "reasoning_effort" => "medium"}
                  role == :admin -> %{"model" => "o3-mini", "reasoning_effort" => "high"}
                end
              )
  end

This compiles without any problems, and my reading of Ash.Resource — ash v3.29.3 is that any return type, including :map, is allowed.

However, I get the following error when executing this code, where victor is a User resource:

victor |> Ash.load!(:llm_model)
[debug] QUERY ERROR source="users" db=2.3ms queue=1.4ms idle=1593.8ms
SELECT u0."id", (CASE WHEN u0."role"::varchar::varchar = $1::varchar::varchar THEN $2 WHEN u0."role"::varchar::varchar = $3::varchar::varchar THEN $4 WHEN u0."role"::varchar::varchar = $5::varchar::varchar THEN $6 ELSE NULL END) FROM "users" AS u0 WHERE (u0."id"::uuid::uuid = $7::uuid::uuid) [:basic, %{"model" => "gpt-4o-mini"}, :pro, %{"model" => "o3-mini", "reasoning_effort" => "medium"}, :admin, %{"model" => "o3-mini", "reasoning_effort" => "high"}, "ab2ba555-671f-4d73-861d-c9171ec7ec9b"]
↳ anonymous fn/3 in AshPostgres.DataLayer.run_query/2, at: lib/data_layer.ex:785
** (Ash.Error.Unknown)
Bread Crumbs:
  > Error returned from: FiveWhys.Accounts.User.read

Unknown Error

* ** (DBConnection.EncodeError) Postgrex expected a binary, got %{"model" => "gpt-4o-mini"}. Please make sure the value you are passing matches the definition in your table or in your query or convert the value accordingly.
<...>

I understand it that there is an attempt to evaluate the expression in Postgresql level, which understandably fails.

Am I expecting too much from calculated fields here? I can of course split it into two calculated fields, e.g., model and reasoning_effort, but I’m curious what is the idiomatic Ash solution here.

Marked As Solved

zachdaniel

zachdaniel

Creator of Ash

This is a bug. Can you open an issue on the ash_postgres repo with a link to a reproduction?

Also Liked

vkryukov

vkryukov

Yes, upgraded everything and can confirm that the bug is still there:

Upgraded:
  ash 3.5.2 => 3.5.4
  ash_admin 0.13.3 => 0.13.4
  ash_authentication 4.6.3 => 4.6.4
  ash_authentication_phoenix 2.5.2 => 2.5.3
  ash_graphql 1.7.6 => 1.7.7
  ash_phoenix 2.1.25 => 2.1.26
  ash_postgres 2.5.14 => 2.5.15
  ash_sql 0.2.66 => 0.2.67

Created a ticket: Calculated field with :map return type doesn't work with Postgres backend · Issue #528 · ash-project/ash_postgres · GitHub

Where Next?

Popular in Questions Top

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: &lt;h1&gt;Create Post&lt;/h1&gt; &lt;%= ...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call t...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I’m a nov...
New

Other popular topics Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41539 114
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
Lily
In templates/appointment/index.html.eex: &lt;%= for appointment &lt;- @appointments do %&gt; &lt;tr&gt; &lt;td&gt;&lt;%= appoi...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New

We're in Beta

About us Mission Statement