clayschick

clayschick

How to use Distinct, Select, and Where in Ecto query?

I’m trying to create a simple query to select distinct values from a column. If I only use select and distinct I get back a list of unique values.

Users
|> select([u], u.group_id)
|> distinct(true)
|> Repo.all()

Which gives me back a list of ids as expected:

[42, 43, nil]

However, when I add a where clause to exclude the nil value I get back a different result.

Users
|> where([u], not is_nil(u.group_id))
|> select([u], u.group_id)
|> distinct(true)
|> Repo.all()

This returns the value:

'*+'

I would have expected the query to return of a list of integers without a nil value. When I run the generated raw SQL in psql I get back the list as expected.

How can I get back a list of distinct group_ids that does not include the nil value?

Marked As Solved

gregvaughn

gregvaughn

You’ve hit the charlist problem. Print your result with:

IO.inspect(result, charlists: :as_lists)

What you’re getting is a list of 2 integers. But they both happen to be printable ascii characters, which is how erlang represents strings. The single quotes are one hint.

Also Liked

rvirding

rvirding

Creator of Erlang

You wait till you get burned with strange binaries. :grinning: They are both the case of not really having strings and using other datatypes to implement them.

sorentwo

sorentwo

Oban Core Team

Don’t worry, this happens to everybody at some point. Erlang has been around quite a while and charlists will keep rearing their bizarre head.

Where Next?

Popular in Questions Top

New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
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
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New

Other popular topics Top

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
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
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 41989 114
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement