Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elixir / Ecto / Postgres Select multiple columns as one

I just want do execute an Ecto query with an simple concatination of two or more columns.

I think the following elixir pseudo code already shows what I try to do:

customers = Customer.undeleted(
  from c in Customer,
  select: %{id: c.id, name: c.name <> " – " <> c.street},
  order_by: c.name
) |> Repo.all

It drives me crazy cuz in SQL it is easy like this: ...SELECT c.id, concat(c.name, ' - ', c,street) AS name

Any ideas how to solve this with ecto querys ?

like image 517
Chilian Avatar asked Apr 24 '17 16:04

Chilian


1 Answers

You cannot use <> in a select expression in Ecto. If you want to call concat like that, you can use fragment:

select: %{id: c.id, name: fragment("concat(?, ' - ', ?)", c.name, c.street)},
like image 147
Dogbert Avatar answered Nov 11 '22 12:11

Dogbert