Unexpected Ecto 2.2 behavior

Hi everyone,

I have the following query:

pry(70)> q2 = from(pop in Epr.Payments.PurchaseOrdersPayments, where: pop.order_id == ^9, join: p in assoc(pop, :payment_order), select: p.id)
#Ecto.Query<from p0 in Epr.Payments.PurchaseOrdersPayments,
 join: p1 in assoc(p0, :payment_order), where: p0.order_id == ^9, select: p1.id>

When I call:

pry(71)> Repo.all(q2)
'G'

When I call:

pry(75)> Repo.one(q2)
71

Shouldn’t I get a list with just one item?

I have noticed that this happen only when I have a single result from the query.

When the query result is more than 1, I get the list with all the items.

Thanks in advance.

1 Like

It is returning a list with one item with a value of 71. A list of a value of 71 is the ASCII value of “G.” When you call Repo.one/1 a list is not returned, and the integer 71 is returned.

iex> [71]
'G'
4 Likes

Oh, thanks.

Can you point me out any resources that I can read about this “issue”? I personally read a lot and haven’t read about it.

Thanks again,

Best regardas

Some resource you can have a look


4 Likes

Thanks @technicalcapt for the resources.

I mentioned information about Ecto displaying results in ASCII.

I think this is not Ecto but the terminal console iex session. Am I correct?

Thanks

When Elixir/Erlang sees a character list that is human readable it will print out corresponding letters instead of the integer values – if that makes any sense. For example:

iex> [102, 111, 111, 98, 97, 114] = 'foobar'
'foobar'

So let us say your query returned an ID of 34202, which has no human readable value associated with it. Therefore you would have seen:

pry(71)> Repo.all(q2)
[34202]

pry(72)> Repo.one(q2)
34202

So nothing odd is really going on here. Your query is returning 71, but when it returns 71 in a list, Elixir is seeing it as a chartist, knows it is human-readable and prints a G, because:

iex> 'G' = [71]
'G'

Hope this helps explain what is going on. :grinning:

3 Likes

Thanks for your time and clarification.

Best regards,

2 Likes

You are welcome. Any time. :+1:

1 Like