Opened 18 years ago

Closed 17 years ago

#3083 closed defect (fixed)

MySQL IntegrityError Exception on object creation

Reported by: anonymous Owned by: nobody
Component: Database layer (models, ORM) Version:
Severity: normal Keywords:
Cc: Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

When two users try to register _simulaneously_ with the same username, they both pass validation (because at that moment nobody of them is registered), but when we do actual user.save(), one of them gets MySQL IntegrityError (Duplicate Key Error). Yes, I can handle it,

from MySQLdb import IntegrityError
try:
    user.save()
except IntegrityError:
    errors['username'] = 'User with this username already exists',

but there is dependence to MySQL. I can't simply do "except:", because there can be another trouble, such as no connectivity to database during restart, but user would get confusing message about invalid username. The same thing is with all models.

Change History (6)

comment:1 by Adrian Holovaty, 18 years ago

Component: Core frameworkDatabase wrapper

comment:2 by mir@…, 18 years ago

Component: Database wrapperAdmin interface
Triage Stage: UnreviewedDesign decision needed

I propose to do it like this, so there's no need to interprete database error messages:

for i in range(settings.RETRIES) 
   ... validate ... 
   try: 
     ... save ... 
     break 
   except ... 
     ... roll back ... 

But perhaps someone has a better idea. If there's a good pattern, maybe the generic views could use it, too?

Since there's a multitude of ways to solve this and it is not clear how to proceed (Adrian has rejected the above solution), I only put this into "Design decision needed".

Some references from django-users:

comment:3 by mir@…, 18 years ago

Component: Admin interfaceDatabase wrapper

Sorry, I didn't see that Adrian put it into "Database Wrapper" category. It could be there, or generic views, or admin ;-)

comment:4 by enlight, 18 years ago

I'm not a fan of Django magically handling IntegrityError(s), there are different ways to handle them and picking just one won't be very flexible, though I don't use generic views for my add/edit/update so I'm a bit biased. Like I mentioned on Django-users I think the best approach to this is simply to remove the dependency on the db module by making IntegrityError available directly through django.db, I'll submit a patch that does this soon.

comment:5 by jordanl, 17 years ago

Doesn't #3450 resolve this ticket?

comment:6 by Jacob, 17 years ago

Resolution: fixed
Status: newclosed

jordanl: yes, it does.

Note: See TracTickets for help on using tickets.
Back to Top