Updating data in M:N relation
See original GitHub issueI 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:
- Created 4 years ago
- Comments:6 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I can confirm this bug.
It will be fixed via a big refactoring that we are currently doing.
Additional context from slack:
Indeed, you are right. Works fine. Thank you!