Fragment to Ecto.Enum

135 views
Skip to first unread message

Allen Wyma

unread,
Nov 4, 2021, 1:55:15 AM11/4/21
to elixir-ecto
Hi,

I'm stuck at an issue. I have a virtual field that I setup as an Ecto.Enum, and I'm trying to make that value into an atom. I have a "friend_status" field, and if there's a friend request that's not been accepted, then it's pending, if it's accepted, then they are friends, and if no field, then they are basically strangers.

I have my case statement working great in a fragment, but it's returning the strings instead of loading the atom. Here's a sample:
from(
...
select: %{ u | friend_status: fragment("CASE WHEN ? THEN 'stranger' ELSE 'pending' END", is_nil(f.id))}
)

I tried using Ecto.Query.API.type/2 function, but I'm not sure what to put it as, since when I tried just the column name, it doesn't work, it returns an error saying it's a virtual field.

Is there a way to make this work?

José Valim

unread,
Nov 4, 2021, 3:20:37 AM11/4/21
to elixi...@googlegroups.com
Type does expect a field as second argument:

type(..., u.friend_status)

Would that work?

--
You received this message because you are subscribed to the Google Groups "elixir-ecto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/97e8ae1e-e088-4ac8-97ca-c654090bc06cn%40googlegroups.com.

Allen Wyma

unread,
Nov 4, 2021, 5:03:38 AM11/4/21
to elixi...@googlegroups.com
No, I mentioned this in my message before, I got an error saying that
because the field is a virtual field, it won't be able to work.
> You received this message because you are subscribed to a topic in the Google Groups "elixir-ecto" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/topic/elixir-ecto/O0KRUZf5Ssw/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to elixir-ecto...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/CAGnRm4JMoAN89reKY30szQizMpsVujpkC09VRmz7kBp1LLrGfA%40mail.gmail.com.

José Valim

unread,
Nov 4, 2021, 6:51:36 AM11/4/21
to elixi...@googlegroups.com
Sorry, my morning reading failed me. Can you please open up an issue?

Allen Wyma

unread,
Nov 4, 2021, 7:57:11 AM11/4/21
to elixi...@googlegroups.com
Yes, I can do that, now. I was going to do that earlier but wanted to
make sure this wasn't already able to be solved.

PS. I apologize if my last message came off as rude.
> To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/CAGnRm4JEE222YJtQ1fR50bmzVAM%2By4m5tfSbQikVYbQDR4kC3Q%40mail.gmail.com.

Allen Wyma

unread,
Nov 4, 2021, 7:59:43 AM11/4/21
to elixi...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages