Opened 15 years ago

Closed 15 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: no UI/UX: no

Description

Using Django 1.1 with pyscopg2. I am using the email-confirmation app (http://github.com/jtauber/django-email-confirmation/) 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):
        try:
            email_address = self.create(user=user, email=email)
            EmailConfirmation.objects.send_confirmation(email_address)
            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\models.py" in add_email
  23.             email_address = self.create(user=user, email=email)
File "c:\python26\lib\site-packages\django\db\models\manager.py" in create
  126.         return self.get_query_set().create(**kwargs)
File "c:\python26\lib\site-packages\django\db\models\query.py" in create
  315.         obj.save(force_insert=True)
File "c:\python26\lib\site-packages\django\db\models\base.py" in save
  410.         self.save_base(force_insert=force_insert, force_update=force_update)
File "c:\python26\lib\site-packages\django\db\models\base.py" in save_base
  495.                     result = manager._insert(values, return_id=update_pk)
File "c:\python26\lib\site-packages\django\db\models\manager.py" in _insert
  177.         return insert_query(self.model, values, **kwargs)
File "c:\python26\lib\site-packages\django\db\models\query.py" in insert_query
  1087.     return query.execute_sql(return_id)
File "c:\python26\lib\site-packages\django\db\models\sql\subqueries.py" in execute_sql
  320.         cursor = super(InsertQuery, self).execute_sql(None)
File "c:\python26\lib\site-packages\django\db\models\sql\query.py" in execute_sql
  2369.         cursor.execute(sql, params)
File "c:\python26\lib\site-packages\django\db\backends\util.py" 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 by Alex Gaynor, 15 years ago

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