Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 594917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:00:19+00:00 2026-05-13T16:00:19+00:00

This code is supposed to get or create an object and update it if

  • 0

This code is supposed to get or create an object and update it if necessary. The code is in production use on a website.

In some cases – when the database is busy – it will throw the exception “DoesNotExist: MyObj matching query does not exist”.

# Model:
class MyObj(models.Model):
    thing = models.ForeignKey(Thing)
    owner = models.ForeignKey(User)
    state = models.BooleanField()
    class Meta:
        unique_together = (('thing', 'owner'),)

# Update or create myobj
@transaction.commit_on_success
def create_or_update_myobj(owner, thing, state)
    try:
        myobj, created = MyObj.objects.get_or_create(owner=user,thing=thing)

    except IntegrityError:
        myobj = MyObj.objects.get(owner=user,thing=thing)
        # Will sometimes throw "DoesNotExist: MyObj matching query does not exist"

    myobj.state = state
    myobj.save()

I use an innodb mysql database on ubuntu.

How do I safely deal with this problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:00:19+00:00Added an answer on May 13, 2026 at 4:00 pm

    This could be an off-shoot of the same problem as here:

    Why doesn't this loop display an updated object count every five seconds?

    Basically get_or_create can fail – if you take a look at its source, there you’ll see that it’s: get, if-problem: save+some_trickery, if-still-problem: get again, if-still-problem: surrender and raise.

    This means that if there are two simultaneous threads (or processes) running create_or_update_myobj, both trying to get_or_create the same object, then:

    • first thread tries to get it – but it doesn’t yet exist,
    • so, the thread tries to create it, but before the object is created…
    • …second thread tries to get it – and this obviously fails
    • now, because of the default AUTOCOMMIT=OFF for MySQLdb database connection, and REPEATABLE READ serializable level, both threads have frozen their views of MyObj table.
    • subsequently, first thread creates its object and returns it gracefully, but…
    • …second thread cannot create anything as it would violate unique constraint
    • what’s funny, subsequent get on the second thread doesn’t see the object created in the first thread, due to the frozen view of MyObj table

    So, if you want to safely get_or_create anything, try something like this:

     @transaction.commit_on_success
     def my_get_or_create(...):
         try:
             obj = MyObj.objects.create(...)
         except IntegrityError:
             transaction.commit()
             obj = MyObj.objects.get(...)
         return obj
    

    Edited on 27/05/2010

    There is also a second solution to the problem – using READ COMMITED isolation level, instead of REPEATABLE READ. But it’s less tested (at least in MySQL), so there might be more bugs/problems with it – but at least it allows tying views to transactions, without committing in the middle.

    Edited on 22/01/2012

    Here are some good blog posts (not mine) about MySQL and Django, related to this question:

    http://www.no-ack.org/2010/07/mysql-transactions-and-django.html

    http://www.no-ack.org/2011/05/broken-transaction-management-in-mysql.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 437k
  • Answers 437k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use another function to create a new scope: for(var button… May 15, 2026 at 4:11 pm
  • Editorial Team
    Editorial Team added an answer Yes that is possible, but it took me some effort.… May 15, 2026 at 4:11 pm
  • Editorial Team
    Editorial Team added an answer Top 200 tags in StackOverflow might be a good place… May 15, 2026 at 4:11 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.