Using An SQL Alias In An Elixir Ecto Query
I recently struggled with converting the following SQL query for use with Elixir’s Ecto library:
What this query does is grab all topics
along with whether or not the given user
has liked the topic
.
The complication came from the fourth select field where there’s an alias involved:
How does one represent this in Ecto? After diving into the docs and code I came up with the following:
The key is the use of the fragment
function in a Map passed as the second argument to the select
function.
The downside to this solution is that you need to explicitly declare the selected fields, rather than just taking all fields from the topic
model and appending the user_does_like
. This way the query would be immune to most changes in the data model. For example it would be nice if the following also worked and grabbed all fields in the topic
model along with appending user_does_like
:
I suspect there is a way to accomplish this. Please email me if you know.