On Mon, Oct 15, 2012 at 3:28 PM, Karen Tracey <kmtra...@gmail.com> wrote: > On Mon, Oct 15, 2012 at 9:43 AM, Tom Evans <tevans...@googlemail.com> wrote: >> >> On Mon, Oct 15, 2012 at 1:50 PM, Zheng Li <dllizh...@gmail.com> wrote: >> > get_or_create shouldn't give duplicate entry error >> > anyone help. >> > >> >> This is incorrect, and a common misconception: get_or_create() is not >> atomic. If this happens, you will get duplicate key errors: > > > No, get_or_create IS intended to be atomic. >
I can be intended to be whatever it wants, but since there is no atomic database operation to get or create a tuple, there is no guarantee of atomicity. >> >> >> Thread A calls get_or_create(a='a', b='b') >> Thread B calls get_or_create(a='a', b='b') >> Thread A get_or_create looks for entry in DB, not found >> Thread B get_or_create looks for entry in DB, not found >> Thread B creates entry in DB >> Thread A get_or_create tries to create entry in DB > > > Internally, get_or_create code in thread A will now attempt to GET the > object created by thread B. It will not automatically reflect the error to > the caller. See: > > https://github.com/django/django/blob/stable/1.4.x/django/db/models/query.py#L430 I regularly - once every few months - see get_or_create raise IntegrityError due to duplicate key on MySQL 5.0/MyISAM/Django 1.3. So whilst that is the intention of the code, it isn't what happens. I'm happy to blame MySQL. Cheers Tom -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com. To unsubscribe from this group, send email to django-users+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.