question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Updating data in M:N relation

See original GitHub issue

I have two models:

model Post {
  id            String         @id @default(cuid())
  title         String
  categories    Category[]
}

model Category {
  id      String        @id @default(cuid())
  name    String
  posts   Post[]
}

Category is defined before the post is created. Creating post itself is fine, the connections in _CategoryToPost table are created. The problem arises when trying to update the post categories. What happens is that the old ones are not deleted and the new ones are added to the _CategoryToPost table. So if I update post and I don’t make any change to the post categories, they duplicate in the _CategoryToPost table.

The code for updating is following:

                const updatedPost = await photon.posts.update({
                    where: { id: args.id },
                    data: {
                        title: args.title,
                        categories: {
                            connect: args.categories.map(categoryId => ({
                                id: categoryId
                            }))
                        }
                    }
                });

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
pantharshit00commented, Aug 26, 2019

I can confirm this bug.

It will be fixed via a big refactoring that we are currently doing.

Additional context from slack:

image

1reaction
cypczcommented, Sep 6, 2019

Indeed, you are right. Works fine. Thank you!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Updating many to many relationships in Entity Framework Core
Updating a many-to-many relationship · Connected state: This is where the load of the data and the update is done in one go,...
Read more >
4.10. Updating a DataSet with a Many-to-Many Relationship
This method updates all changes in the DataSet back to the data source by calling in the correct order the Update( ) method...
Read more >
Update and Remove Many-to-Many Relationships - SQLModel
Now we'll see how to update and remove these many-to-many relationships. We'll continue from where we left off with the previous code. Full...
Read more >
Update the data that has many to many relationship - Laracasts
Update the data that has many to many relationship. Hi,. I have 2 models which are Developers and Projects. A developer can have...
Read more >
Updating A Many-To-Many Relationship In Django
The post and request data are then passed to the PostSerializer , and the request data is validated. We save the post to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found