Alvinkariuki

Alvinkariuki

Ecto Query Returns unwanted data

I am trying to write a query that gets all users matching a criteria

The postgresql query is as follows

select id from users WHERE account_id  IS NOT NULL LIMIT 10 OFFSET 10;

in this example, the number of users that match the given criteria are 12, meaning that for this query, only two user ids will be returned since it skips by 10 rows. This brings the correct data.

I have written the same query in ecto as follows

from(u in User, where: not is_nil(u.account_id), limit: 10, offset: 0 , select: u.id) |> Repo.all() 

expected response:

{:ok, [11, 12]}

actual response:

'\f\r'

what is going on?

Marked As Solved

dimitarvp

dimitarvp

They are the same, you’re simply seeing them formatted as characters because iex is trying to be helpful.

Run this in iex[11, 12] == '\f\r'

Also Liked

sodapopcan

sodapopcan

It’s a subtle syntactic difference and has been the cause of many questions. If you upgrade to the latest version of Elixir, charlists are now output using the new ~c sigil which is much easier to catch. You would have seen ~c"\f\r".

sodapopcan

sodapopcan

Repo.all() returns a “bare” list, not an :ok/:error tuple.

Because you are only selecting ids that are each less than 255, they are being output as a charlist. The IDs in your case happen to correspond to the ascii characters \f and \r.

EDIT: or [11, 12] as @dimitarvp more helpfully said :slight_smile:

dimitarvp

dimitarvp

Oops yeah, OP still needs to make sure the whole thing is wrapped in an :ok tuple indeed.

Wrong, see my next comment.

Where Next?

Popular in Questions Top

Tee
can someone please explain to me how Enum.reduce works with maps
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
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
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
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
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
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
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
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
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
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
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
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