I’m trying to do a grouped count of achievements by using a Postgres CASE
statement (I’ll be adding a couple more clauses once I get the query to work), but I’m having trouble with the data casting.
Here’s my query:
from a in Schemas.Achievement,
where: a.user_id == ^user_id,
select:
{fragment(
"""
CASE
WHEN ? IN (?) THEN 'a'
END as "type"
""",
a.achievement_slug,
type(^slugs, {:array, AchievementEnum})
), count()},
group_by: fragment("type")
AchievementEnum
is a postgres enum type (achievement
)
This results in the following error:
[error] Task #PID<0.3036.0> started from #PID<0.2534.0> terminating
** (Postgrex.Error) ERROR 42883 (undefined_function) operator does not exist: achievement = achievement[]
I’m guessing that ecto isn’t sending the list of slugs as a list (so it is sent as achievement
instead of achievement[]
).
But if I manually put WHEN ? = ('slug1', 'slug2') THEN 'a'
, it works