Shmuel
01/04/2022, 8:23 PMawait prisma.role.update({
where: { id: 1 },
data: {
users: {
create: [
{ user: { connect: { id: 1 } } },
{ user: { connect: { id: 2 } } },
{ user: { connect: { id: 3 } } },
]
}
}
})
How would I remove records from a many to many?
I can do it like this...
await prisma.role.update({
where: { id: 1 },
data: {
users: {
deleteMany: [
{ user_id: 1 },
{ user_id: 2 },
{ user_id: 3 },
]
}
}
})
But is there a similar way of doing it like the first example just with disconnect?Maciek K
01/04/2022, 9:13 PMawait prisma.implementationRole.update({
where: { id: 1 },
data: {
users: {
disconnect: [{ id: 1 }, { id: 2 }, { id: 3],
},
},
})
Your first example is also a little strange. Can you explain more what do you want to achieve?Shmuel
01/04/2022, 9:49 PMusers.0.user.id That case is similar to what the docs mention. I think my case is different since it's a many to many with a join table. Your case works for a one to many like user to posts.
In my first example. I have a roles table with a users table and then a join table of user_roles
A User as roles and a Role has users . I my first example I'm creating user_role records by connecting an existing user to a role .
In my 2nd example I would like to delete user_role records by disconnecting the existing user . I don't know if this is possible, but it sounds like the inverse of my first example, which is why I thought it should be feasible.Maciek K
01/05/2022, 6:58 AMShmuel
01/05/2022, 8:11 PMmodel User {
id Int @id @default(autoincrement())
first_name String @db.VarChar(50)
last_name String @db.VarChar(50)
roles UserRoles[]
@@map("users")
}
model Role {
id Int @id @default(autoincrement())
name String @unique @db.VarChar(50)
users UserRoles[]
@@map("roles")
}
model UserRoles {
user User @relation(fields: [user_id], references: [id], onDelete: NoAction, onUpdate: NoAction)
user_id Int
role Role @relation(fields: [role_id], references: [id], onDelete: NoAction, onUpdate: NoAction)
role_id Int
@@id([user_id, role_id])
@@map("user_roles")
}Maciek K
01/06/2022, 5:29 PMdisconnect is only for relations which are optional. Those records in UserRoles should be deleted anyway, since join table has both relations required.
So either use your nested deleteMany or just like this:
await prisma.userroles.deleteMany({
where: {
userId: { in: [1,2,3] },
roleId: 1
}
});Shmuel
01/06/2022, 7:13 PM