Opened 8 years ago

Closed 8 years ago

#5367 closed (worksforme)

sqlite3+ManyToManyField: unable to link to 1000 (or more) objects.

Reported by: Jakub Wilk <django@…> Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


With DATABASE_ENGINE = 'sqlite3' and the following foo/

from django.db import models

class Foo(models.Model):

class Bar(models.Model):
        foos = models.ManyToManyField(Foo)

I try:

>>> from foo import models
>>> models.Foo.objects.all().count()
>>> bar = models.Bar(1)
>>> bar.foos = models.Foo.objects.all()[:1000]
Traceback (most recent call last):
  File "<console>", line 1, in ?
  File "/var/lib/python-support/python2.4/django/db/models/fields/", line 460, in __set__
  File "/var/lib/python-support/python2.4/django/db/models/fields/", line 287, in add
    self._add_items(self.source_col_name, self.target_col_name, *objs)
  File "/var/lib/python-support/python2.4/django/db/models/fields/", line 338, in _add_items
    [self._pk_val] + list(new_ids))
  File "/var/lib/python-support/python2.4/django/db/backends/", line 19, in execute
    return self.cursor.execute(sql, params)
  File "/var/lib/python-support/python2.4/django/db/backends/sqlite3/", line 129, in execute
    return Database.Cursor.execute(self, query, params)
OperationalError: too many SQL variables

Change History (3)

comment:1 Changed 8 years ago by ubernostrum

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

This feels like a built-in limit of SQLite; you may need to break this up into multiple operations.

comment:2 Changed 8 years ago by deepak <deep.thukral@…>

Worked without any problem

>>> from abc.models import *
>>> for i in range(2000): Foo().save()
>>> print Foo.objects.all().count()
>>> bar = Bar(1)
>>> bar.foos = Foo.objects.all()[:1000]

I'd suggest you to post SQL queries fired by django.

comment:3 Changed 8 years ago by deepak <deep.thukral@…>

  • Resolution set to worksforme
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.
Back to Top