Trying to get relations from one table to another, as one-to-many but get only one value
I have a one-to-many relation on a table, and I want to get all the workoutResults associated with a workoutSession. My query looks like this:
I don't understand why workoutSession.workoutResult is not an array, since there are many
workoutResults
with the same workoutSessionId
I hope I'm clear enough.
I've also tried adding the .all()
at the end of the query, but this does not seem to exist anywhere in drizzle 🤔12 Replies
same issue. 😦
Can you show how are you defining your tables and relations?
Thanks for your response.
User Table:
Address Table:
Fetching data:
When you do an inner join, the shape of the result will be the shape that the driver returns. Drizzle won't do any data transformation.
The query that you showed, will return an array of objects with all the columns from users and addresses all together.
The crud API is designed to be transparent from SQL -> JS
If you want a user with an array of addresses, I would suggest you use the relations API
If i go with this approach i am getting the following error: "Cannot read properties of undefined (reading 'referencedTable')"
am i missing something that i didn't noticed?
Schema is as above i mentioned earlier.
Are you passing your schema to the drizzle object when defined?
In the examples, they always pass the schema to the drizzle function
Yes,
@databse/schema
All of that seems ok
Do you have a reproduction repo?
found the error.
i didnt export the relation schema
it's working
@angelelz Thanks for your valuable time. I appreciate your help ❤️ .
This works fine, for me it was the relation in the schema that wasn't well made after running introspect.
A side issue I get from using queries now, is the fact that I don't see how you can chain where using that method.. Any example of using multiple
where
in accessing data using queries? Thanks a lot!i think you can use 'and' operator.