Opened 9 years ago

Closed 9 years ago

#12144 closed (wontfix)

Django throwing ProgrammingError instead of IntegrityError for Duplicate Key with Postgres

Reported by: tghw Owned by: nobody
Component: Uncategorized Version: 1.1
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


Using Django 1.1 with pyscopg2. I am using the email-confirmation app ( to make sure changes to email addresses are to the right account. In that app is the following code:

class EmailAddressManager(models.Manager):
    def add_email(self, user, email):
            email_address = self.create(user=user, email=email)
            return email_address
        except IntegrityError:
            return None

class EmailAddress(models.Model):
    class Meta:
        unique_together = (
            ("user", "email"),

When add_email() is called with a user/email combination that already exists in the database, a ProgrammingError is thrown for the duplicate key instead of raising an IntegrityError, as should be the case:

File "c:\python26\lib\site-packages\django_email_confirmation-0.1.3-py2.6.egg\emailconfirmation\" in add_email
  23.             email_address = self.create(user=user, email=email)
File "c:\python26\lib\site-packages\django\db\models\" in create
  126.         return self.get_query_set().create(**kwargs)
File "c:\python26\lib\site-packages\django\db\models\" in create
File "c:\python26\lib\site-packages\django\db\models\" in save
  410.         self.save_base(force_insert=force_insert, force_update=force_update)
File "c:\python26\lib\site-packages\django\db\models\" in save_base
  495.                     result = manager._insert(values, return_id=update_pk)
File "c:\python26\lib\site-packages\django\db\models\" in _insert
  177.         return insert_query(self.model, values, **kwargs)
File "c:\python26\lib\site-packages\django\db\models\" in insert_query
  1087.     return query.execute_sql(return_id)
File "c:\python26\lib\site-packages\django\db\models\sql\" in execute_sql
  320.         cursor = super(InsertQuery, self).execute_sql(None)
File "c:\python26\lib\site-packages\django\db\models\sql\" in execute_sql
  2369.         cursor.execute(sql, params)
File "c:\python26\lib\site-packages\django\db\backends\" in execute
  19.             return self.cursor.execute(sql, params)

Exception Type: ProgrammingError at /profile/
Exception Value: duplicate key value violates unique constraint "emailconfirmation_emailaddress_user_id_key"

It looks to me like that app is doing the right thing, but Django is throwing the wrong error.

Change History (1)

comment:1 Changed 9 years ago by Alex Gaynor

Resolution: wontfix
Status: newclosed

Django isn't throwing any exceptions here, it's being thrown by the postgres driver you're using. Django has no control over this.

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