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

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
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
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
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 ...
New
mathew4509
I have a list say x = ["23gh", "56kh", "97mh"] I would like to pass each element to Val in each iteration. Say, in iteration 1 -------...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New

Other popular topics Top

yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
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 ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
danschultzer
None of the current solutions worked well for me, so I went ahead and built a user management system from scratch. This project took far...
548 27727 240
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New

We're in Beta

About us Mission Statement