class Parent(models.Model):
pass
class Child(models.Model):
name = models.CharField()
parent = models.ForeignKey(Parent, on_delete=models.CASCADE, related_name="children")
class Meta:
unique_togetehr = [("name", "parent")]
@strawberry_django.input(Child)
class ChildInput:
name: auto
class ParentType:
id: auto
@strawberry_django.input(Parent)
class ParentInput:
children = Optional[list[ChildInput]]
@strawberry.type
class Mutation:
update_parent: ParentType = strawberry_django.mutations.create(types.ParentInput)
Assuming a Parent with ID 1 but no child exists this works on the first attempt only:
Consider the following setup:
Assuming a
Parent
with ID 1 but no child exists this works on the first attempt only:On the second attempt an error is raised:
This is due to the
update_m2m
creating new objects, even if the data is uniquely identifying an existing relation.Proposed solution: Use
manager.get_or_create
instead ofmanager.create
.Upvote & Fund