I'm using Postgres for a chat app. There I have two tables, one with the conversation ids and participants, and another table with the messages.
I'm running the following query to get all the conversations for a user, and I use a lateral join to include the last message on the results:
select c.id, c.title, c.members, max(m.created_at) delivered_at, last_message
from conversations c
join messages m on c.id = m.conversation_id
left join lateral (select message from messages m where c.id = m.conversation_id order by created_at desc limit 1) last_message on true
where array[4] <@ c.members
group by c.id, last_message
order by delivered_at desc
which works, but the message is returned as (message)
and not message
and I don't know why, really. Is there anyting I can do so I don't have to replace the parenthesis as an additional step?