1

I'm new in phoenix/elixir and need help.

I try to place result of a ecto.query in variable like this

owner =
(from ex in "executors",
where: ex.email == ^account_name,
where: ex.pass_hash == ^pwd,
select: ex.id )
|> Repo.all()

And I need 'owner' to be an integer like ex.id, but it is like a char - '\a', 'M', etc.

How correctly get result from query with integer type or how transform it from codepoint to integer?

thanks in advance

Onorio Catenacci
  • 14,928
  • 14
  • 81
  • 132
dnl008
  • 11
  • 3
  • It is not clear what you are asking for, please edit your question is. Please edit your question to make it clear what your question is? – Tshilidzi Mudau Aug 11 '17 at 13:37
  • 2
    Possible duplicate of [Elixir lists interpreted as char lists](https://stackoverflow.com/questions/30037914/elixir-lists-interpreted-as-char-lists) – Patrick Oscity Aug 11 '17 at 14:32
  • Thank you for your answer, but I'm using that variable no with inspect, I try to make another query and using 'owner' as condition, which need to be integer – dnl008 Aug 11 '17 at 15:40
  • And what happens if you use `owner` in another query? Did you mean to use `Repo.one` instead of `Repo.all`? – Dogbert Aug 11 '17 at 16:04
  • Yes, @Dogbert, `Repo.one` I should use. Such as mudasobwa says it return single result instead a list. List of integers is converted to list of `codepoint` and if I use `codepoint` such as parametr where I need `integer` it makes error - wrong parametr – dnl008 Aug 12 '17 at 07:57

1 Answers1

2

TL;DR

[owner | _] =
    (from ex in "executors",
     where: ex.email == ^account_name,
     where: ex.pass_hash == ^pwd,
     select: ex.id )
    |> Repo.all()

Explanation:

Repo.all() returns a list of integers, not a single integer. Which is interpreted as a charlist when you examine it. If you are positive there is a single result, perform a match as above, or even:

[owner] = ...

Or, even better:

owner = (<QUERY>)
        |> Repo.one()
Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
  • Thank you for your aswers. @mudasobwa answer was most helpfull. `[owner] =...` and `Repo.one()` work for me. – dnl008 Aug 12 '17 at 07:52