PyMan skrev:
> Hi all :)
>
> I have the following problem.
>
> I have a function that do some get_or_create on model X giving as
> parameters the field Y and Z. The same function is running on
> different threads, so it can happen that more get_or_create on model X
> and fields Y/Z are called at the same time. It can also happen that
> the values of Y/Z are the same in the different threads and here it
> comes the problem: it happens that get_or_create says it returned more
> than one row for X-Y/Z.
>
> Now Y/Z are not unique in my model and I do not have an overloading of
> the save function, but I think I can have the same problem if Y/Z were
> unique too.
>
> Looking at the DB I have more than one row with same values...how
> comes this? Here the get_or_create...
>   
Pitching in, since it seems no-one was giving the answer I would give -
though Benjamin Slavin comes close.

Threads and thread saftey has nothing to do with your problem, What you
have is a problem with two different processes (or threads, doesn't
matter) which are issuing database commands that get in the way of each
other.

What happens is something like this (P1, P2 and P3 are two differnt
processes, or threads):

P1: Does a row with the values Y and Z exist? No
P2: Does a row with the values Y and Z exist? No
P1: Add a row with Y and Z
P2: Add a row with Y and Z

P3: Does a row with the values Y and Z exist: AssertionError

This can happen if you are using transactions or not - doesn't really
matter (though transactions will make the problem worse).

Solution: Do not use get_or_create with fields that are not unique in
the database. Do NOT rely on your application to enforce uniqueness.

This still has the problem that get_or_create can return an error (the
second insert in the above sequence will fail) but at least your data
stays correct.

Nis

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To post to this group, send email to django-developers@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to