Jakub Figlak
03/16/2022, 7:34 PMmodel Organization {
id String @id @default(cuid())
name String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
// Relation fields
members User[]
}
I'm gonna create a route to create an organization that accepts body:
name: string
memberIds: string[]
So basically I want to create relations from the many
side of the many-to-one
relation (user can be a member of only one organization at the given point in time). And here's my question - what is the best way to ensure that? Do I need to query for all users, check if they're already members of other organization and if they're, reject an entire request? Or is there a simpler way?andrewicarlson
03/21/2022, 6:29 PMconst members = await prisma.members.findMany({
where: {
id: {
in: memberIds
},
organization: {
isNot: null
}
}
})
Note that since you're querying from the 'one' side of a 'many-to-one' you need to check the null value using isNot
instead of using the none
property.