-
Notifications
You must be signed in to change notification settings - Fork 117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WritableNestedModelSerializer and unique_together #49
Comments
Hi @isasiluis28 Can you add a test which reproduces the bug? |
Sure @ir4y This is the object we have created Then if we try to update creating a new object for producto_detalles (without the ID field, this is because we want to delete the other objects), but with same ID of veterinaria that is already there before the update (which by the way is the one with a constraint) Then it raises an IntegrityError I think the problem is that is trying to create the new reverse relations without deleting the previous ones, so at that point we still have the previous objects, and when it tries to create the new relations it will raise an IntegrityError beacause of the existance of an object that already satisfies the constraint. Looking at the code that handles the updates in NestedUpdateMixin I found that first tries to create or update the new relations, then deletes the relations that are no longer needed, doesn't it have to be the other way around? meaning first delete the relations, then create the new ones? |
I mean fork |
Hello @isasiluis28! Thank you for the contribution. But I think it is possible to support this validator. |
Is there any progress on this? |
@ruscoder , any advice on how to implement this? I'd give it a go. |
UniqueFieldsMixin does not work with unique_together for me I have to send an empty list that deletes the items first then send the new items I guess the way it works by default, it deletes the previous nested items and creates a new ones, we should await and confirm the items are deleted before sending in new items otherwise it'll raise an IntegrityError |
The only way I can get this to work is by passing an ID or making one of my unique fields a primary key. |
Is there any progress? Is unique_together validator going to be supported in one of the next releases? |
@ruscoder |
I hit a wall on this as well. |
Hi. Unfortunately, I haven't used drf-writable-nested for more than 6 years and I can't give a hint on how to implement it properly without diving into the implementation, but according to the discussion above there's a workaround suggested in #50 that has one limitation, but it might be not relevant for someone who affected by this issue. So, I can recommend something like
that is actually copy-paste of original update with changed order of invocation of delete_reverse_relations_if_need and update_or_create_reverse_relations (according to the suggestions from #50). In general, if someone would like to solve a conflict between #50 and #36 it might be merged into the main branch. |
WritableNestedModelSerializer won't work when there is a foreign key relation with a unique constraint con the child model.
I already tried the mixin UniqueFieldsMixin, but it does not work either.
The text was updated successfully, but these errors were encountered: