#24009: get_or_create can raise an IntegrityError in race conditions -------------------------------+-------------------- Reporter: lanzz | Owner: nobody Type: Uncategorized | Status: new Component: Uncategorized | Version: 1.7 Severity: Normal | Keywords: Triage Stage: Unreviewed | Has patch: 0 Easy pickings: 0 | UI/UX: 0 -------------------------------+-------------------- `get_or_create` is described as being atomic in the documentation, but that is not the case. Since it first tries to retrieve an existing record and if that fails it tries to create a new one, another thread can create that record in the small interval between the two operations. This will not result in data corruption as the database will enforce its constraints, but it makes the whole convenience method not that convenient, if I also need to handle this in a `try` block around its invocation. If I _need_ to do `try: get_or_create() except IntegrityError: get()` anyway, it makes no sense to use `get_or_create` in the first place, that logic would work if I just try to create the entity and only retrieve it if the create raises an `IntegrityError`.
When I call `get_or_create` I expect it to return the existing record, or to create a new record if one does not exist yet. Getting an `IntegrityError` there is like saying "there's no existing record, but I cannot create a new one because one already exists". -- Ticket URL: <https://code.djangoproject.com/ticket/24009> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/048.508297bc6868daa9db65feae58c2d89c%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.