#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.

-- 
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/054.0ff9c016386487dad0aa6710b5645141%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to