Duplicate rows with same key in DB?

45 views
Skip to first unread message

ydjango

unread,
Jun 22, 2012, 1:23:32 AM6/22/12
to django...@googlegroups.com
I have code as

view_a(request):
  .... Populate my_a, my_b and my_c...
  try:
      tp = TP.objects.get(a__exact = my_a, b__exact = my_b)
  except TP.DoesNotExist, e:
     tp = TP()
     tp.a = my_a
     tp.b = my_b
     tp.c = my_c
     tp.save()
  else:
     tp.c = my_c
     tp.save()

This view is called by ajax calls. TP is a django model and mysql db.
I am using django 1.1.4/ python2.5/mod_wsgi2.5,

I find that in, one case, two rows with same value of  a and b has been created in DB. ( value of c was different)
What could have caused it?

Any clues or hints on how to track down this problem or what to look for will be appreciated.

Daniel Roseman

unread,
Jun 22, 2012, 10:52:49 AM6/22/12
to django...@googlegroups.com
You probably have a race condition. You should use the built-in `get_or_create` manager method, which helps to minimize that sort of thing (it's not perfect, but it's better than coding it yourself).
--
DR.

Kurtis Mullins

unread,
Jun 22, 2012, 11:01:21 AM6/22/12
to django...@googlegroups.com
You could also put a unique_together statement in your Model. If you do this, you'll need to update your database as well to enforce the constraint at the database level.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To view this discussion on the web visit https://groups.google.com/d/msg/django-users/-/HTccZPk06t8J.

To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

ydjango

unread,
Jun 22, 2012, 6:02:42 PM6/22/12
to django...@googlegroups.com

Thank you, I also believe it is Race condition. I do not want to use get_or_create as I like to have control over DB writes and updates. I will try Unique_together and catch the integrity error exception as Kurtis suggested or check again before doing save().

or just thinking aloud, Is there a way to acquire a lock which can serialize any other requests which have same "a" and "b" values?

Melvyn Sopacua

unread,
Jun 23, 2012, 3:52:19 AM6/23/12
to django...@googlegroups.com
On 23-6-2012 0:02, ydjango wrote:
>
> Thank you, I also believe it is Race condition. I do not want to use
> get_or_create as I like to have control over DB writes and updates. I will
> try Unique_together and catch the integrity error exception as Kurtis
> suggested or check again before doing save().
>
> or just thinking aloud, Is there a way to acquire a lock which can
> serialize any other requests which have same "a" and "b" values?

unique_together + transaction middleware.
https://docs.djangoproject.com/en/1.4/topics/db/transactions/
--
Melvyn Sopacua


Melvyn Sopacua

unread,
Jun 23, 2012, 3:57:31 AM6/23/12
to django...@googlegroups.com
Ok, hit send too soon. No, this doesn't give you access to other
requests as far as I can tell, but operations depending upon the insert
operation succeeding are now part of a transaction and can be handled
more gracefully inside django as well, pending the result of the
transaction.

--
Melvyn Sopacua


Cal Leeming [Simplicity Media Ltd]

unread,
Jun 23, 2012, 5:33:25 AM6/23/12
to django...@googlegroups.com
Also - get_or_create doesn't always prevent a race condition if you have high traffic.. depending on your code+stack.

I already did a post about this a while back:



--
Melvyn Sopacua


--
You received this message because you are subscribed to the Google Groups "Django users" group.
Reply all
Reply to author
Forward
0 new messages