This post is the top search result for both implicit and explicit disconnect many to many. If you want to completely disconnect an implicit many to many:
const data = await prisma.group.update({
where: {
id: groupId,
},
data: {
users: {
set: [],
},
},
});
Gotten from here: https://github.com/prisma/prisma/issues/5946
This post is the top search result for both implicit and explicit disconnect many to many. If you want to completely disconnect an implicit many to many:
const data = await prisma.group.update({
where: {
id: groupId,
},
data: {
users: {
set: [],
},
},
});
Gotten from here: https://github.com/prisma/prisma/issues/5946
With explicit many-to-many relation you can just delete from the table that represents the relation (i.e. UsersGroups in your case):
prisma.usersGroups.delete({
where: { userId_groupId: { groupId: groupId, userId: userId } },
});
If you want to delete multiple users from a group:
prisma.usersGroups.deleteMany({
where: { groupId: groupId, userId: { in: users.map((user) => user.id) } },
});
I want to implement a LIKE POST feature in my app using express and Prisma ORM. Is it possible to chain the connect and disconnect for the table relation.
This is my current code, but it can only connect the relation.
But what I want to do is, if the current user (req.user.id) hits the same endpoint, then disconnect the relation.
const likedPost = await prisma.like.update({
where: {
postId: req.params.postId,
},
data: {
liker: { connect: { userId: req.user.id } },
},
include: {
post: true,
},
});Any kind of help is highly appreciated. Thanks
Pass falsy values to disconnect a relation
explicit m:n relationships incorrect handling of disconnect
How can I disconnect a relation by using it's fields?
Overide entire relation field with Prisma? - Stack Overflow
You can use set replace connect
data: {
groups: {
set: {
id: "3"
}
}
}
I believe what you're looking for is an API similar to the one of scalar lists:
mutation {
createUser(data: {
scores: { set: [1, 2, 3] }
friends: { set: ["Sarah", "Jane"] }
throws: { set: [false, false] }
}) {
id
}
}
This is currently not possible with Prisma but there already is an open feature request for that functionality, please leave your 👍 if you're interested in that feature.
I have a Prisma request in which I'm getting an array of tags (a tag is just a string) that I have to use in an update request.
Here's the request:
await prisma.node.update({
where: { id, tenantId: tenantId as string },
data: {
question: {
update: {
where: { id: questionId as string },
data: {
text: text as string,
slug: slug as string,
user: { connect: { id: userId } },
},
},
},
tags: {
updateMany: tags.map((tag: string) => {
return {
where: { id: tag, tenantId },
data: { id: tag, tenantId },
};
}),
},
},
});
The problem is that I can only add tags but not remove them so if I have one tag I can add another one fine but after that I can't remove it.
I tried to use updateOrCreate but it was not working.
What should I use to make the removal of a tag work ?