#36818: AttributeError: 'NoneType' object has no attribute 'getlimit' when calling
bulk_create as the first database operation on SQLite
-------------------------------------+-------------------------------------
Reporter: guro_ishiguro | Type: Bug
Status: new | Component: Database
| layer (models, ORM)
Version: 6.0 | Severity: Normal
Keywords: sqlite bulk_create | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
== Summary ==
In Django 6.0, using `bulk_create` with an SQLite backend causes an
`AttributeError` if it is the first database operation performed after
`django.setup()`. This occurs because `bulk_create` attempts to determine
the `max_query_params` by calling `sqlite3.Connection.getlimit()`, but the
underlying database connection has not yet been established.
== Environment ==
* '''Django Version:''' 6.0
* '''Python Version:''' 3.11+ (Reproducible on 3.13)
* '''Database:''' SQLite
== Minimal Reproducible Example ==
{{{
import os
import django
from django.conf import settings
from django.db import models
if not settings.configured:
settings.configure(
DATABASES={
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": ":memory:",
}
},
INSTALLED_APPS=["__main__"],
)
django.setup()
class MyModel(models.Model):
name = models.CharField(max_length=100)
class Meta:
app_label = "__main__"
if __name__ == "__main__":
MyModel.objects.bulk_create([MyModel(name="test")])
print("finished without error")
}}}
== Traceback ==
{{{
AttributeError: 'NoneType' object has no attribute 'getlimit'
File "django/db/models/query.py", line 833, in bulk_create
returned_columns = self._batched_insert(...)
File "django/db/models/query.py", line 1937, in _batched_insert
max_batch_size = max(ops.bulk_batch_size(fields, objs), 1)
File "django/db/backends/sqlite3/operations.py", line 47, in
bulk_batch_size
return self.connection.features.max_query_params // len(fields)
File "django/db/backends/sqlite3/features.py", line 152, in
max_query_params
return
self.connection.connection.getlimit(sqlite3.SQLITE_LIMIT_VARIABLE_NUMBER)
AttributeError: 'NoneType' object has no attribute 'getlimit'
}}}
== Suspected Cause ==
In `django/db/backends/sqlite3/features.py`, the `max_query_params`
property accesses `self.connection.connection` (the raw DB-API connection)
without ensuring that a connection has been opened.
Django typically establishes connections lazily. However, `bulk_create`
logic for SQLite now depends on `getlimit()`, which requires an active
connection. Since this is accessed before any SQL is actually executed,
the connection object is still `None`.
== Workaround ==
Calling `connection.ensure_connection()` or performing any other database
query (e.g., `MyModel.objects.exists()`) before calling `bulk_create`
resolves the issue.
--
Ticket URL: <
https://code.djangoproject.com/ticket/36818>
Django <
https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.