drf-writable-nested icon indicating copy to clipboard operation
drf-writable-nested copied to clipboard

in NestedUpdateMixin would be better to delete_reverse before update_or_create_reverse

Open dz0 opened this issue 7 years ago • 7 comments

I think, if we have unique validator (let's say for field x), and before request we had some related items {"id":1, "x": 10}, {"id":2, "x": 5}

and in request { "id":42, "stuf":"bar", "related": [ {"id":2, "x": 10} ] }

here we delete id=1, but the other "takes over" his value. (ps.: not tested)

so maybe switch the lines?

        self.update_or_create_reverse_relations(instance, reverse_relations)
        self.delete_reverse_relations_if_need(instance, reverse_relations)

dz0 avatar Sep 07 '18 10:09 dz0

totally agree, this is a duplicated of my issue #49 , take a look I am using the way you are saying (switching the lines) and works perfectly.

isasiluispy avatar Sep 13 '18 22:09 isasiluispy

@isasiluispy @dz0 Hello! Thanks for the contribution. It really makes sense. I'll prepare a fix soon.

ruscoder avatar Sep 14 '18 06:09 ruscoder

I've found that your solution breaks some important tests.

ruscoder avatar Sep 14 '18 07:09 ruscoder

I've fixed another bug in delete method.

Now, only one test is broken when I switch update and delete: WritableNestedModelSerializerTest.test_update_reverse_one_to_one_without_pk

This test covers functionality from this PR https://github.com/beda-software/drf-writable-nested/pull/36

ruscoder avatar Sep 14 '18 08:09 ruscoder

@ruscoder will this bug be fixed ? I think it's not wotking properly yet, for unique_together constraints like this example #49 I still have to swicth the lines to get it working :(

isasiluispy avatar Oct 18 '18 13:10 isasiluispy

@isasiluispy Your suggested fix brakes functionality from #36. I don't know how we can fix it saving backward compatibility for the moment.

As for me, I don't understand the worth of #36, but a few people asked for this feature. If you want, you can make a PR with fix for #36 and for this issue.

ruscoder avatar Oct 23 '18 16:10 ruscoder

To solve this problem, the logic in #36 needs to be moved into its own utility function and update changed to:

        self.materialize_one_to_one_pk(instance, reverse_relations)  # code from #36 relocated here
        self.delete_reverse_relations_if_need(instance, reverse_relations)
        self.update_or_create_reverse_relations(instance, reverse_relations)

Hopefully that helps someone who's motivated. I can't help ATM because I'm just here to evaluate the project as a replacement for our DIY logic.

claytondaley avatar Nov 15 '18 20:11 claytondaley