Many to many relation not enough information
when i query
it tells me
Error: There is not enough information to infer relation "orders.stripeTransactions"what other info could be needed
19 Replies
@jakeleventhal Hey, would you mind including the tables?
(obviously truncated irrelevant fields)
the many to many table was generated via prisma
It'd probably be less confusing if you renamed
a
and b
to orderId
and stripeTransactionsId
respectively, but that's besides the point
This should solve the issue, since a many-to-many relationship is two one-to-many relationships with a table in between:
which would mean querying using:
yeah, i suppose that works
its just a bit confusing having the double nested with
also doesnt really allow me to do things like "only query stripe transactions where
stripeTransaction.isRefund
is false
those came from auto-gend prisma fieldsyeah, that's because prisma creates the pivot table which sits between the many-to-many relationship on two tables... so it's a hidden table from the schema, but it applied to the database
You can definitely do that through the query builder. I'm relatively new to drizzle, so I'm not sure if you can do it using the query api, although I believe I've seen people here filter using a foreign table
Drizzle ORM - next gen TypeScript ORM
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
here
so i guess it would look like this?
that doesnt work
because you cant use
where
conditions on one
relations
and the where really needs to be on the outer stripeTransactions
queryahh you're right, yeah
then you get a really ugly nested query
thats why i thought surely im not doing this correctly
i believe the table and relation definitions are correct, but i haven't experimented enough myself to suggest a solution for filtering
this looks really dumb and creates a lot of overhead
@Angelelz is this the best wya to handle this type of query?
seems like theres gotta be a better way than to query all of the users stripeTransactions
yeah, this would be useful for me to know too :D
I don't think that's dumb, or creates overhead. What gives you that impression?
With the correct indexes, and if this is the structure of the data, this is it
This is how SQL works
The other option is to get the transactions first and then send another query, you might want to benchmark both approaches
When you're using the RQB, you are more limited. The correct way of running this query is with joins. That will be the most performant way
you cant really do joins with the relational query builder though right
the overhead was that it seems like a large subquery
Yeah, to do joins you need to run the queries with the crud api
huh? what do you mean
you mean just like db.select... etc
Yeah
yeah it's just messy with jsonagg etc
i guess a prisma-like many to many with would be nice
basically bypass the pivot table by specifying it in the relations for many to many. then just use
with: { stripeTransactions: true }