Calculate boolean value in select_merge

Hey guys! I got this query:

 q =
      from p in Photo,
        preload: [:model, :category],
        full_join: f in Favorite,
        on: f.photo_id ==,
        select_merge: %{
          like_count: count(,
          liked_by_me: f.user_id == ^

It’s working when I remove the line liked_by_me: f.user_id == ^ , but I need the liked_by_me property to know whether a photo was liked by the current user or not. Another I could do it would be to map over the result and dynamically add the key, but I want to do it using Ecto. The error it’s throwing right now is ** (Postgrex.Error) ERROR 42803 (grouping_error) column "f1.user_id" must appear in the GROUP BY clause or be used in an aggregate function. liked_by_me should be a boolean as defined the schema.

Any help with this? Thanks!

Before the group_by, this is going to have many rows of f for a given - that’s what’s being counted in like_count. You’ll need a corresponding aggregate function to combine those rows of f into a single value for liked_by_me - seems like bool_or would do what you want, but make sure to poke at it to see if it does something reasonable when f.user_id is NULL for some rows.

@al2o3cr hey thanks for answering! I ended up doing it outside of the query in Elixir. I also had to query the database again for the IDs of all the photos liked by the user to compare that with the photo IDs. Which gives me the boolean column I wanted.