[Django] #23554: Unmodified object fails to save because of UNIQUE constraint

2 views
Skip to first unread message

Django

unread,
Sep 24, 2014, 5:00:06 PM9/24/14
to django-...@googlegroups.com
#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.

Django

unread,
Sep 24, 2014, 5:05:59 PM9/24/14
to django-...@googlegroups.com
#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 | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by tinloaf):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

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 '...')

--
Ticket URL: <https://code.djangoproject.com/ticket/23554#comment:1>

Django

unread,
Sep 24, 2014, 5:16:37 PM9/24/14
to django-...@googlegroups.com
#23554: Unmodified object fails to save because of UNIQUE constraint
-------------------------------+--------------------------------------
Reporter: tinloaf | Owner: nobody
Type: Bug | Status: closed
Component: Uncategorized | Version: 1.7
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by tinloaf):

* status: new => closed
* resolution: => invalid


Comment:

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/23554#comment:2>

Reply all
Reply to author
Forward
0 new messages