Hope you guys don’t mind as I asking Prisma question here.
Is there a way to directly add data to the bridge table?
My schema:
model role {
id Int @id @default(autoincrement())
displayName String @db.VarChar(100)
users userRoles[]
createdAt DateTime @default(now())
createdBy Int?
creator user? @relation("RoleCreator", fields: [createdBy], references: [id])
updatedAt DateTime @updatedAt
updatedBy Int?
updater user? @relation("RoleUpdater", fields: [updatedBy], references: [id])
}
model user {
id Int @id @default(autoincrement())
email String @unique
roles userRoles[]
hashedPassword String
salt String
resetToken String?
resetTokenExpiresAt DateTime?
createdAt DateTime @default(now())
createdBy Int?
creator user? @relation("Creator", fields: [createdBy], references: [id])
updatedAt DateTime @updatedAt
updatedBy Int?
updater user? @relation("Updater", fields: [updatedBy], references: [id])
roleCreator role[] @relation("RoleCreator")
roleUpdater role[] @relation("RoleUpdater")
userCreator user[] @relation("Creator")
userUpdater user[] @relation("Updater")
userRoles userRoles[] @relation("RoleAssigner")
}
model userRoles {
role role @relation(fields: [roleId], references: [id])
roleId Int
user user @relation(fields: [userId], references: [id])
userId Int
assignedAt DateTime @default(now())
assignedBy Int
roleAssigner user @relation("RoleAssigner", fields: [assignedBy], references: [id])
@@id([roleId, userId])
}
Currently, there are two rows in the role table, when I create a new user I want to assign roles to them.
My code:
db.user.create({
data: {
email: username,
hashedPassword: hashedPassword,
salt: salt,
userRoles: {
create: [{
roleId: 1,
}]
}
},
})
It is not working, having some typing issues. Any help would be great, thanks.