Opened 4 years ago

Closed 4 years ago

#23554 closed Bug (invalid)

Unmodified object fails to save because of UNIQUE constraint

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


Hi. I have a model (see below) of which I cannot save() an object again after creating it once, the database backend (sqlite) states that the ID violates a unique constraint. See here:

>>> from finance.models import Mandate
>>> m = Mandate.objects.all()[0]
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/mnt/daten/home/tinloaf/src/alumnet/alumnet/finance/", line 149, in save
    super(Mandate, self).save(self, *args, **kwargs)
  File "/mnt/daten/home/tinloaf/src/alumnet/venv/lib/python3.3/site-packages/django/db/models/", line 590, in save
    force_update=force_update, update_fields=update_fields)
  File "/mnt/daten/home/tinloaf/src/alumnet/venv/lib/python3.3/site-packages/django/db/models/", line 618, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/mnt/daten/home/tinloaf/src/alumnet/venv/lib/python3.3/site-packages/django/db/models/", line 699, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "/mnt/daten/home/tinloaf/src/alumnet/venv/lib/python3.3/site-packages/django/db/models/", line 732, in _do_insert
    using=using, raw=raw)
  File "/mnt/daten/home/tinloaf/src/alumnet/venv/lib/python3.3/site-packages/django/db/models/", line 92, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/mnt/daten/home/tinloaf/src/alumnet/venv/lib/python3.3/site-packages/django/db/models/", line 921, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/mnt/daten/home/tinloaf/src/alumnet/venv/lib/python3.3/site-packages/django/db/models/sql/", line 920, in execute_sql
    cursor.execute(sql, params)
  File "/mnt/daten/home/tinloaf/src/alumnet/venv/lib/python3.3/site-packages/django/db/backends/", line 81, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/mnt/daten/home/tinloaf/src/alumnet/venv/lib/python3.3/site-packages/django/db/backends/", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/mnt/daten/home/tinloaf/src/alumnet/venv/lib/python3.3/site-packages/django/db/", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/mnt/daten/home/tinloaf/src/alumnet/venv/lib/python3.3/site-packages/django/utils/", line 549, in reraise
    raise value.with_traceback(tb)
  File "/mnt/daten/home/tinloaf/src/alumnet/venv/lib/python3.3/site-packages/django/db/backends/", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/mnt/daten/home/tinloaf/src/alumnet/venv/lib/python3.3/site-packages/django/db/backends/sqlite3/", line 485, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: UNIQUE constraint failed:

I looked into the database manually and verified that everything is as it is supposed to be, no duplicate PKs or what. Please tell me if there is any further debugging that I can do.

Here is the model:

class Mandate(models.Model):
    user = models.ForeignKey(User)
    holder = models.TextField()
    street = models.CharField(max_length=40)
    plz = models.IntegerField()
    city = models.CharField(max_length=20)
    country = models.CharField(max_length=20)
    iban = IBANField() 
    bic = SWIFTBICField() 
    active = models.BooleanField()
    revoked_on = models.DateField(null=True, blank=True)
    mandate_id = models.CharField(max_length=35, unique=True)

    def save(self, *args, **kwargs):
        if (self.mandate_id is None) or (len(self.mandate_id) == 0):
            self.mandate_id = self.generate_id()
        super(Mandate, self).save(self, *args, **kwargs)

Change History (2)

comment:1 Changed 4 years ago by tinloaf

Got something: I looked at django.db.connection.queries. The first query is when it retrieves the mandates, the second one should be an UPDATE, but for some reason it's an INSERT. What's going on there?

>>> print(connection.queries)
{'time': '0.001', 'sql': 'QUERY = \'SELECT "finance_mandate"."id", "finance_mandate"."user_id", "finance_mandate"."holder", 
"finance_mandate"."street", "finance_mandate"."plz", "finance_mandate"."city", "finance_mandate"."country", "finance_mandate"."iban", 
"finance_mandate"."bic", "finance_mandate"."active", "finance_mandate"."revoked_on", "finance_mandate"."mandate_id" FROM "finance_mandate" 
LIMIT 1\' - PARAMS = ()'}, 

{'time': '0.000', 'sql': "QUERY = 'BEGIN' - PARAMS = ()"}, {'time': '0.000', 'sql': 'QUERY = \'INSERT INTO 
"finance_mandate" ("id", "user_id", "holder", "street", "plz", "city", "country", "iban", "bic", "active", "revoked_on", "mandate_id") 
SELECT %s AS "id", %s AS "user_id", %s AS "holder", %s AS "street", %s AS "plz", %s AS "city", %s AS "country", %s AS "iban", %s AS "bic", 
%s AS "active", %s AS "revoked_on", %s AS "mandate_id"\' - PARAMS = (1, 2, \'...\', \'...\', ..., \'...\', 
\'Deutschland\', \'...\', \'...\', False, None, \'...\')'}]

(Replaced actual data with '...')

comment:2 Changed 4 years ago by tinloaf

Resolution: invalid
Status: newclosed

Ugh, forget it, sometimes I'm braindead (and passing self to a super-call). Strange that it does not blow up though.

Sorry for the noise.

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