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

Fixed #25939 -- Removed transaction from QuerySet.update_or_create's save(). #5804

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions django/db/models/query.py
Expand Up @@ -485,8 +485,7 @@ def update_or_create(self, defaults=None, **kwargs):
for k, v in six.iteritems(defaults):
setattr(obj, k, v)

with transaction.atomic(using=self.db, savepoint=False):
obj.save(using=self.db)
obj.save(using=self.db)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one seems ok to me.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed this one looks fine.

It will have beneficial performance effects if:

  • ATOMIC_REQUESTS is not in use
  • update_or_create ends up creating an object
  • the save method of that objects does long things like sending email after the write (i.e. once it holds an exclusive lock on the database)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This may affect query counts tested by assertNumQueries in TransactionTestCase which should be mentioned in the release notes.

return obj, False

def _create_object_from_params(self, lookup, params):
Expand Down
4 changes: 4 additions & 0 deletions docs/releases/1.10.txt
Expand Up @@ -361,6 +361,10 @@ Miscellaneous
:class:`~django.http.HttpResponse` are now closed immediately instead of when
the WSGI server calls ``close()`` on the response.

* ``obj.save()`` call in ``dlango.db.models.query.update_or_create`` is not
wrapped in transaction.atomic context anymore. This may affect query
counts tested by ``assertNumQueries`` in ``TransactionTestCase``.

.. _deprecated-features-1.10:

Features deprecated in 1.10
Expand Down