Hello everyone! Another question from me. I have o...
# orm-help
j
Hello everyone! Another question from me. I have organization model like so:
Copy code
model 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:
Copy code
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?
a
Hey @Jakub Figlak 👋 I think you're on the right track there. If you already have the `memberId`'s readily available, you could try something like this:
Copy code
const 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.