Here's the syntax for queries with composite key
const deleteRelation = await prisma.labelplaylist.delete({
where: {
playlistId_labelId: {
playlistId: playListIdVariable, //replace with appropriate variable
labelId: labelIdVariable, //replace with appropriate variable
},
},
});
You can read more in the get record by compound ID or compound unique identifier subsection of the CRUD Reference Guide in the Prisma docs. This reference shows reading data, but the where condition is similar for delete and update as well.
mysql - Prisma delete many to many relationship with Composite Key - Stack Overflow
prisma - How to delete a record and any relationship records in an explicit many to many relationship? - Stack Overflow
Disconnecting a Many to Many Relation in Prisma - Stack Overflow
postgresql - Prisma Explicit Many to Many - how to delete item without deleting user - Stack Overflow
Here's the syntax for queries with composite key
const deleteRelation = await prisma.labelplaylist.delete({
where: {
playlistId_labelId: {
playlistId: playListIdVariable, //replace with appropriate variable
labelId: labelIdVariable, //replace with appropriate variable
},
},
});
You can read more in the get record by compound ID or compound unique identifier subsection of the CRUD Reference Guide in the Prisma docs. This reference shows reading data, but the where condition is similar for delete and update as well.
Since it's an explicit many-to-many relationship, nested deleteMany only deletes relation table records, acting like a disconnect. You can then write your query like that:
await req.db.playlist.update({
data: {
labels: {
deleteMany: {},
},
},
where: {
id: labelId,
},
})
or the other way around.
It won't delete your related table records but only the links between them.
Here is the Prisma documentation to disconnect related fields
For single disconnect
const updatePost = await prisma.user.update({
where: {
id: 16,
},
data: {
posts: {
disconnect: [{ id: 12 }, { id: 19 }],
},
},
select: {
posts: true,
},
})
To disconnect all
const updateUser = await prisma.user.update({
where: {
id: 16
},
data: {
posts: {
set: []
}
},
include: {
posts: true
}
})
here you go a way to do that:
const { PrismaClient } = require('@prisma/client')
const prisma = new PrismaClient()
const saveData = async () => {
const fighter1 = await prisma.fighter.create({
data: {
name: 'Ryu',
},
})
const fighter2 = await prisma.fighter.create({
data: {
name: 'Ken',
},
})
console.log('FIGHTERS');
console.log(JSON.stringify(fighter1, null, 2));
console.log(JSON.stringify(fighter2, null, 2));
const fight = await prisma.fight.create({
data: {
name: 'Ryu vs Ken',
fighters: {
createMany: {
data: [
{
fighterId: fighter1.id,
},
{
fighterId: fighter2.id,
},
]
},
},
},
select: {
id: true,
fighters: {
select: {
fighter: true,
},
},
},
});
console.log('FIGHTS');
console.log(JSON.stringify(await prisma.fight.findMany({ include: { fighters: true } }), null, 2));
const fighterFightsToDelete = prisma.fighterFights.deleteMany({
where: {
fightId: fight.id,
}
})
const fightToDelete = prisma.fight.delete({
where: {
id: fight.id,
}
})
await prisma.$transaction([ fighterFightsToDelete, fightToDelete ])
console.log('RESULT');
console.log(JSON.stringify(await prisma.fight.findMany({ include: { fighters: true } }), null, 2));
console.log(JSON.stringify(await prisma.fighter.findMany({ include: { fights: true } }), null, 2));
}
saveData()
And the result is the following :)

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) } },
});