How to join one to many relationship using db.select api and get nested array in result #2228
Unanswered
shellking4
asked this question in
Q&A
Replies: 2 comments
-
I could not succeed and plan to change from mariadb to mysql just to be able to user query syntax to get this nested object. It's very convenient for JS devs. |
Beta Was this translation helpful? Give feedback.
0 replies
-
I don't think you can achieve it with the Even the relational API uses // { id: number; organization: { id: number } }[];
await db.query.studentTable.findMany({
columns: { id: true },
with: { organization: { columns: { id: true } } },
}); db.query.studentTable.findMany({
// ...
}).toSQL().sql select
"id",
(
select
json_array ("id") as "data"
from
(
select
*
from
"organization" "studentTable_organization"
where
"studentTable_organization"."id" = "studentTable"."organization_id"
limit
?
) "studentTable_organization"
) as "organization"
from
"student" "studentTable" |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
So let's suppose I have tables users and posts where a user owns many posts
How to retrieve a user with his posts using db.select and get back a result like
[
{ userId: "sjvhbdsh", posts: [ {"postId": "sdbdjvbsjbv"} ] }
]
And this without any js transformatio, just with specifying fields in the select method
Beta Was this translation helpful? Give feedback.
All reactions