Opened 10 years ago
Closed 10 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 |
Description
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)
Change History (2)
comment:1 by , 10 years ago
comment:2 by , 10 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
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.
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?
(Replaced actual data with '...')