Opened 9 years ago
Closed 9 years ago
#27237 closed Bug (invalid)
Cannot save() model with ForeignKey with sqlite, but works OK with PostgreSQL
| Reported by: | Philippe Ombredanne | Owned by: | nobody |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 1.10 |
| Severity: | Normal | Keywords: | sqlite, foreignkey, save |
| Cc: | Triage Stage: | Unreviewed | |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
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'}]
Change History (2)
comment:1 by , 9 years ago
comment:2 by , 9 years ago
| Resolution: | → invalid |
|---|---|
| Status: | new → closed |
Note:
See TracTickets
for help on using tickets.
My bad the problem also exists with postgres.