Opened 12 years ago

Closed 11 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


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, one of them gets MySQL IntegrityError (Duplicate Key Error). Yes, I can handle it,

from MySQLdb import IntegrityError
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 Changed 12 years ago by Adrian Holovaty

Component: Core frameworkDatabase wrapper

comment:2 Changed 12 years ago by mir@…

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 ... 
     ... save ... 
   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 Changed 12 years ago by mir@…

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 Changed 12 years ago by enlight

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 Changed 11 years ago by jordanl

Doesn't #3450 resolve this ticket?

comment:6 Changed 11 years ago by Jacob

Resolution: fixed
Status: newclosed

jordanl: yes, it does.

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