views:

99

answers:

1

Django's get_or_create function always cause "get() returned more than one Model name" error in a multi-threaded program.

I even tried to put get_or_create statement inside a lock.acquire() and lock.release() block but still didn't work.

The program only works when I set thread_count=1

The database is on InnoDB engine. How to fix this kind of problem?

+1  A: 

This is not caused by multithreading, but because there are more than one object in database, that satisfies your query. You must select exactly one object from the database using get, otherwise it will raise an exception.

gruszczy
@gruszczy, how to "select exactly one object from the database using get", the get() function is supposed to select only one object but the question is how to prevent more than one objects to be created in above case.
jack
You must create proper query. The easiest way is of course SomeModel.objects.get(pk=5), because this gives you exactly one hit. But maybe you can prepare condition, that can be also satisfied by exactly one row.
gruszczy