[Django] #27237: Cannot save() model with ForeignKey with sqlite, but works OK with PostgreSQL

4 views
Skip to first unread message

Django

unread,
Sep 18, 2016, 12:04:31 PM9/18/16
to django-...@googlegroups.com
#27237: Cannot save() model with ForeignKey with sqlite, but works OK with
PostgreSQL
-------------------------------------+-------------------------------------
Reporter: pombredanne | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.10
(models, ORM) | Keywords: sqlite, foreignkey,
Severity: Normal | save
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Using Django 1.10.1 on Python 2.7.6 on Ubuntu 14.04, I have these simple
models:

{{{
class A(models.Model):
text = models.TextField(null=True)

class A2(models.Model):
a = models.ForeignKey(A)
}}}

with PostgreSQL 9.5.4 and psycopg2 things work as expected:
{{{
>>> a=A()
>>> a2=A2(a=a)
>>> a.save()
>>> a2.save()
>>> A2.objects.all()[0]
<A2: A2 object>
>>> A2.objects.all()[0].id
1
}}}
and I have these queries:
{{{
>>> from pprint import pprint as p
>>> from django import db
>>> p(db.connections.all()[0].queries)
.......

{u'sql': u'INSERT INTO "packagedcode_a" ("text") VALUES (NULL) RETURNING
"packagedcode_a"."id"',
u'time': u'0.001'},
{u'sql': u'INSERT INTO "packagedcode_a2" ("a_id") VALUES (NULL) RETURNING
"packagedcode_a2"."id"',
u'time': u'0.000'}]
}}}

If I switch to sqlite3 or (pysqlite):

{{{
>>> a=A()
>>> a2=A2(a=a)
>>> a.save()
>>> a2.save()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/pombreda/tmp/local/lib/python2.7/site-
packages/django/db/models/base.py", line 796, in save
force_update=force_update, update_fields=update_fields)
File "/home/pombreda/tmp/local/lib/python2.7/site-
packages/django/db/models/base.py", line 824, in save_base
updated = self._save_table(raw, cls, force_insert, force_update,
using, update_fields)
File "/home/pombreda/tmp/local/lib/python2.7/site-
packages/django/db/models/base.py", line 908, in _save_table
result = self._do_insert(cls._base_manager, using, fields, update_pk,
raw)
File "/home/pombreda/tmp/local/lib/python2.7/site-
packages/django/db/models/base.py", line 947, in _do_insert
using=using, raw=raw)
File "/home/pombreda/tmp/local/lib/python2.7/site-
packages/django/db/models/manager.py", line 85, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/home/pombreda/tmp/local/lib/python2.7/site-
packages/django/db/models/query.py", line 1045, in _insert
return query.get_compiler(using=using).execute_sql(return_id)
File "/home/pombreda/tmp/local/lib/python2.7/site-
packages/django/db/models/sql/compiler.py", line 1054, in execute_sql
cursor.execute(sql, params)
File "/home/pombreda/tmp/local/lib/python2.7/site-
packages/django/db/backends/utils.py", line 79, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/home/pombreda/tmp/local/lib/python2.7/site-
packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/home/pombreda/tmp/local/lib/python2.7/site-
packages/django/db/utils.py", line 94, in __exit__
six.reraise(dj_exc_type, dj_exc_value, traceback)
File "/home/pombreda/tmp/local/lib/python2.7/site-
packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
File "/home/pombreda/tmp/local/lib/python2.7/site-
packages/django/db/backends/sqlite3/base.py", line 337, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: NOT NULL constraint failed:
packagedcode_a2.a_id

}}}
and I have these queries:
{{{

>>> from pprint import pprint as p
>>> from django import db
>>> p(db.connections.all()[0].queries)
.......
{u'sql': u'BEGIN', u'time': u'0.000'},
{u'sql': u'INSERT INTO "packagedcode_a" ("text") VALUES (NULL)',
u'time': u'0.000'},
{u'sql': u'BEGIN', u'time': u'0.000'},
{u'sql': u'INSERT INTO "packagedcode_a2" ("a_id") VALUES (NULL)',
u'time': u'0.000'}]

}}}

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

Django

unread,
Sep 18, 2016, 12:58:50 PM9/18/16
to django-...@googlegroups.com
#27237: Cannot save() model with ForeignKey with sqlite, but works OK with
PostgreSQL
-------------------------------------+-------------------------------------
Reporter: pombredanne | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution:
Keywords: sqlite, foreignkey, | Triage Stage:
save | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

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


Comment:

My bad the problem also exists with postgres.

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

Django

unread,
Sep 18, 2016, 12:59:22 PM9/18/16
to django-...@googlegroups.com
#27237: Cannot save() model with ForeignKey with sqlite, but works OK with
PostgreSQL
-------------------------------------+-------------------------------------
Reporter: pombredanne | Owner: nobody
Type: Bug | Status: closed

Component: Database layer | Version: 1.10
(models, ORM) |
Severity: Normal | Resolution: invalid

Keywords: sqlite, foreignkey, | Triage Stage:
save | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

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


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

Reply all
Reply to author
Forward
0 new messages