Is this a good way to fetch all of the users inside this relational table?
Hi guys,
Is this the best way to have an endpoint that gets all of my "Workspaces"? (I want to get all of the Workspaces a user is in, based on the UserWorkspace table I have)
I don't like how I had to pass in that long array type to userWorkspace constant. Is there a better way to do this?
10 Replies
Here´s my prisma model btw:
I think your
UserWorkspace
model is redundant, Prisma can create those join tables for you if you configure your relations properly. Doing that would make the query a lot simpler.
As for the query you have, I don't see why you'd need to put a custom type there? TS Should be able to infer the return type of the query.Oh, you're right. I removed it and now it is working
It was not working while I was writing it I think
Lmao I thought that this was the way of doing a many-to-many relationship
How do I build the two models then? (User and Workspace), to have a many-to-many relation
This is from the prisma docs:
I think I found it?
Yep, that's it. Custom join tables are definitely useful in the case that you have extra data you want to associate to the relation, but for the case you shared implicit m2m relations should be good
Thank you so much!!
Just for documentation of this question: here´s how I adapted it for the implicit version:
Prisma schema:
Procedure:
gorgeous!