Monday, October 15, 2012

Re: get_or_create gave IntegrityError: (1062, "Duplicate entry) error sometimes

On Mon, Oct 15, 2012 at 1:50 PM, Zheng Li <dllizheng@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:

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

Where I say 'thread', you could also read 'process'. If you are
capable of serving multiple requests simultaneously, this can happen,
even if you aren't using threads.

If this is a problem for you, use a transaction aware DB instead of
MySQL. It won't solve the issue, but at least you will be able to
unroll the transaction.

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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate