Chris Tsongas
11/09/2021, 7:10 PMconst getUser = await prisma.user.update({
where: {
id: 9
},
data: {
posts: {
connect: {
id: 11
},
create: {
title: "My new post title"
}
}
}
})
Unfortunately, I don't believe this will disconnect previously related posts i.e. a post with an id
of 5
that should no longer be related to this user.
This means that (in this example) when the API receives an array of post IDs that should be related to this user, I first have to query which posts are currently related to the user, then run another query to connect new posts, and yet another query to disconnect old posts.
Think about a situation where on the front end, someone can edit a product and assign it to zero or more categories via a multi-select. Same problem.
Seems unfortunate there's no way to update a many-to-many relation in one operation, or maybe I've missed it? I know you can connect an array of IDs, but again I'm guessing that won't disconnect IDs that aren't in the array, or all IDs if the array is empty.Maciek K
11/09/2021, 7:23 PMChris Tsongas
11/09/2021, 7:27 PM