go-gorm / gorm

The fantastic ORM library for Golang, aims to be developer friendly
https://gorm.io
MIT License
37.02k stars 3.94k forks source link

Ability to perform “preloading” for Postgresql in a single query #7228

Open meftunca opened 1 month ago

meftunca commented 1 month ago

Your Question

Do we have the ability to do “preloading” in a single query? We can get data in a single query in the form of Postgresql lateral join (select Array(...query)). If Gorm supports this, it would be really perfect for me :)

Expected answer

select f.*, array_to_json(questions.list) from survey.forms f
left join lateral(
    select Array(
           select to_jsonb(*) from survey.form_items fi where fi.form_id = f.id
           ) as list
    ) questions on true;