DEV Community 👩‍💻👨‍💻

Goran Jakovljevic
Goran Jakovljevic

Posted on

Prisma ORM update explicit many to many relations

I was struggling to find this in docs. So, when you have explicit many to many relationship, lets say you have post that has multiple tags. And you want to edit that post and pass up new tags or edit/remove existing ones. This is the way to do it:

const response: jobs = await prisma.posts.update({
            data: {
                users: { connect: { id: } },
                posts_tags: {
                    deleteMany: {},
                    create: => ({
                        tags: { connect: { id: tag } },
            where: {
                slug: postSlug,
Enter fullscreen mode Exit fullscreen mode

So first you pass up deleteMany: {}, which will delete all connections between post and tags. Then you are assigning/connecting new ones.

Top comments (2)

yash49 profile image
Yash Shah

I also did the same thing, If someone finds a better approach then please provide it by comment.

leanfj profile image
Leandro Ferreira de Jesus

Tnaks a lot bro!

Timeless DEV post

Git Concepts I Wish I Knew Years Ago