Reputation: 2865
I'm new to Prisma and while it's been incredibly easy to pick up so far, I'm running into a problem that I can't seem to find a good answer to. I've read through the docs about relation queries, but from my understanding Prisma doesn't have any support for many-to-many via fluent api. Every query must return a single entity and then you can add the related table, but in my case my query will return many entities that I then would like to join on.
Here's a quick example of my schema:
model User {
id String @id @default(uuid())
}
model Workspace {
id String @id @default(uuid())
}
model WorkspaceUser {
workspace Workspace @relation(fields: [workspaceId], references: [id])
workspaceId String
user User @relation(fields: [userId], references: [id])
userId String
}
I was hoping to do something like:
await prisma.workspaceUser.findMany({
where: { userId: "123" },
}).workspaces();
Also, I noticed that intellisense shows there is a .join() method, but it's not mentioned in the docs.
Does Prisma offer a solution to this problem, or should I use $queryRaw
?
Upvotes: 7
Views: 8633
Reputation: 4191
You can use that exact syntax for single selects aka
await prisma.workspaceUser.findUnique({
where: { userId: "123" },
}).workspace();
For many selections it will be returned as an array but should still have the related entity if you specify include
const allRecords = await prisma.workspaceUser.findMany({
where: { userId: "123" },
include: {workspace: true},
});
Then you can do something like allRecords[i].workspace;
Upvotes: 10