hey guys! I am trying to implement an AuthProvide...
# orm-help
c
hey guys! I am trying to implement an AuthProvider model for our postgres prisma schema. You can see the start of my work here:
Copy code
model AuthProvider {
  id String @id @default(cuid())

  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt

  type           AuthProviderType 
  externalUserId String?          @unique 

  email         String  @unique // this should not be unique if the userId is the same among two AuthProviders
  emailVerified Boolean? @default(false) 

  password String? // Hashed password

  user   User   @relation(fields: [userId], references: [id])
  userId String

  @@map("auth_providers")
}
A user can have many authentication providers, such as email + password, google, twitter... that they can use to sign in with Now here comes the tricky part: what I want to accomplish is to make it so an AuthProviders email is unique, but ONLY if the userId of that AuthProvider is different. So a user should be able to have several AuthProviders with the same email, but if one user tries to connect an authprovider with an email that another user already has connected (on any of their authproviders), then it should fail via unique constraint. would this be achieveable just using uniuqe constraints/indexes in the schema?
👀 1
n
Hey! So if I understand correctly, you want the combination of
externalUserId
and
email
as unique?
v
👋 Hello @Christoffer - can you give us a little more information here? Let us know if this is still an open question!