#23554: Unmodified object fails to save because of UNIQUE constraint
-------------------------------+--------------------
     Reporter:  tinloaf        |      Owner:  nobody
         Type:  Bug            |     Status:  new
    Component:  Uncategorized  |    Version:  1.7
     Severity:  Normal         |   Keywords:
 Triage Stage:  Unreviewed     |  Has patch:  0
Easy pickings:  0              |      UI/UX:  0
-------------------------------+--------------------
 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]
 >>> m.id
 1
 >>> m.save()
 Traceback (most recent call last):
   File "<console>", line 1, in <module>
   File "/mnt/daten/home/tinloaf/src/alumnet/alumnet/finance/models.py",
 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/base.py", 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/base.py", 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/base.py", 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/base.py", 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/manager.py", 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/query.py", 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/compiler.py", 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/utils.py", 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/utils.py", 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/utils.py", 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/six.py", 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/utils.py", 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/base.py", line 485, in execute
     return Database.Cursor.execute(self, query, params)
 django.db.utils.IntegrityError: UNIQUE constraint failed:
 finance_mandate.id

 }}}

 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)
 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/23554>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/050.447ceb9d50565fe976d64a8db52dafb7%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to