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 , 18 years ago
Component: | Core framework → Database wrapper |
---|
comment:2 by , 18 years ago
Component: | Database wrapper → Admin interface |
---|---|
Triage Stage: | Unreviewed → Design decision needed |
comment:3 by , 18 years ago
Component: | Admin interface → Database 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 , 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.
I propose to do it like this, so there's no need to interprete database error messages:
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: