Skip to content
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

Atomic transaction #48

Open
ruscoder opened this issue Aug 24, 2018 · 5 comments · May be fixed by #101
Open

Atomic transaction #48

ruscoder opened this issue Aug 24, 2018 · 5 comments · May be fixed by #101
Labels

Comments

@ruscoder
Copy link
Member

Now, to avoid partial object creation you should wrap your root serializer's save() into transaction.atomic.

I'm not sure about wrapping save() in the library in each serializer with mixin. What do you think?

@ruscoder
Copy link
Member Author

@kenny1992 suggested to determine the root serializer depending on serializer.parent

@claytondaley
Copy link
Contributor

We use a ModelViewSet mixin to wrap create, update and destroy in a transaction. We do this because we use signals for some critical (but intentionally decoupled) logic. If we don't wrap these calls, a failed signal handler won't roll back the actual database change.

The opposite is also possible. Someone may consider the data critical even if a signal were to fail. Forcing them to wrap the save method in a transaction would not be backwards compatible as they would lose that data.

In general, I'm +1 on wrapping calls in transactions. It strikes me as the safe default. If someone has to improve their code (by handling errors in signal handlers) to upgrade drf-writable-nested, that's not a bad thing. The few people who need to disable the feature (e.g. for performance reasons) should be skilled enough to undo the change.

@denizdogan
Copy link

I'd love to see this happen as it's not obvious to me how to implement the feature in the current state of the library.

@denizdogan
Copy link

Wouldn't adding something like this be enough?

class AtomicNestedCreateMixin(NestedCreateMixin):
    def save(self, **kwargs):
        with transaction.atomic():
            return super().save(**kwargs)

@szhouhao
Copy link

Wouldn't adding something like this be enough?

class AtomicNestedCreateMixin(NestedCreateMixin):
    def save(self, **kwargs):
        with transaction.atomic():
            return super().save(**kwargs)

厉害!

@claytondaley claytondaley linked a pull request Feb 1, 2020 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants