I am trying to build query that gets data from 3 different tables using sqlalchemy
I tried
select(
models.Animals.id,
models.Animals.description,
models.AnimalTypes.name.label('type'),
func.coalesce(
func.array_agg(
func.jsonb_build_object(
'id',
models.Animals.id
)
), None
).label('animals')
)
.join(models.FamilyTypes, models.Family.id == models.Family.family_type_id)
.outerjoin(models.Animals, models.Animals.campaign_id == models.Animals.id)
.group_by(models.Family.id, models.FamilyType.name)
i expect to get animals:null
instead of animals:{"id": null}
2
Answers
Solved this using
case
Not an answer, just a longer comment. Using
psql
on a local test table. Usingjsonb_agg
from Aggregate functions: