zgy
zgy
PPrisma
Created by zgy on 8/30/2024 in #help-and-questions
Nested update when creating single record
Hi Prisma-Community, I want to set two fields to null when I create a user but I have no idea if this is possible.
model Membership {
id String @id @default(cuid())
roles Role[]
user User?
organization Organization @relation(fields: [organizationId], references: [id])
organizationId String
invitedById String?
invitedEmail String? @unique

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

@@unique([organizationId, invitedEmail])
}
model Membership {
id String @id @default(cuid())
roles Role[]
user User?
organization Organization @relation(fields: [organizationId], references: [id])
organizationId String
invitedById String?
invitedEmail String? @unique

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

@@unique([organizationId, invitedEmail])
}
I want to set invitedById and invitedEmail to null when I create a user and connect it to the membership. This is the code that works for creating a user and connect the membership relation.
const session = await prisma.session.create({
data: {
expirationDate: getSessionExpirationDate(),
user: {
create: {
email: email.toLowerCase(),
membership: {
connect: {
organizationId,
invitedEmail: email,
},
},
password: {
create: {
hash: hashedPassword,
},
},
},
},
},
select: { id: true, expirationDate: true },
});
const session = await prisma.session.create({
data: {
expirationDate: getSessionExpirationDate(),
user: {
create: {
email: email.toLowerCase(),
membership: {
connect: {
organizationId,
invitedEmail: email,
},
},
password: {
create: {
hash: hashedPassword,
},
},
},
},
},
select: { id: true, expirationDate: true },
});
Is it possible to set the invitedById and invitedEmail to null in the same query?
2 replies