I would like the "user_names" to be a list of users, but I can't get multiple subquery items to be assigned to "user_names". It work's if the subquery only returns 1 item, but doesn't work if it returns multiple.
Bed Table
[id]
[1]
[2]
[3]
Assignment Table:
[id, bed_id, user_id]
[1, 1, 1]
[2, 1, 2]
Users Table:
[id, 'user_name']
[1, 'John Smith']
[2, 'Jane Doe']
sql = "SELECT
b.id,
(
SELECT
u.user_name
FROM
assignments AS a
INNER JOIN
users as u
ON
a.user_id = u.id
WHERE a.bed_id = b.id
) AS user_names
FROM beds AS b"
The desired results would be:
[1, 'John Smith, Jane Doe']
[2, '']
[3, '']
I tried hardcoding the bed id and running this segment to get a list of names. It didn't work:
sql = """
(
SELECT
array_agg(user_name)
FROM
roomchoice_assignment AS a
INNER JOIN
roomchoice_customuser as u
ON
a.user_id = u.id
WHERE
a.bed_id = 1
GROUP BY user_name
)"""
It returned the following:
[
[
[
"John Smith"
]
],
[
[
"Jane Doe"
]
]
]
I was hoping for this:
['John Smith, Jane Doe']