Select data from related table as well
I have 2 tables:
Whenever I select a
bar
I would also like to return the associated foo.fooData
. I believe I need a leftJoin for that, but how does that work when doing an update
or create
query?
E.g. how would the following snippet also return the associated foo
?
10 Replies
In SQL you can't insert/update on 2 different table at the same time. You need to use 2 separate queries, one per table, preferably inside a transaction. See here: https://orm.drizzle.team/docs/transactions
Transactions – DrizzleORM
Drizzle ORM | %s
We don't know yet if drizzle will implement a convenience API in the future to do something like this.
So I would have to do:
?
(I don't need to insert/update 2 different tables at the same time, I would like to return data from 2 tables)
Oh, I understand. You want to update one table but then return a join?
Yes, you would do it like this. Although not necessarily in a transaction.
In this case, you can just run the update, make sure it went well, and then return the joined data
yes exactly!
If you have any example of that, that would be awesome 😛
you can even use the RQB to return the join
How stable is the API for this? From the docs I've read they looked like a 'nice-to-have' and am not sure if it's worth going through it since it's just 1 level of querying
Thank you!!
I mean it is stable as far as I know
well guess I'll just go work in relation queries 😛
Is there some way to re-use the type of
result
? If there is a result I want to pass it to a function that transforms it to a DTO
. But result doesn't typecheck as a Bar
or a NewBar