[Django] #36818: AttributeError: 'NoneType' object has no attribute 'getlimit' when calling bulk_create as the first database operation on SQLite

17 views
Skip to first unread message

Django

unread,
Dec 22, 2025, 4:03:33 AM12/22/25
to django-...@googlegroups.com
#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.

Django

unread,
Dec 22, 2025, 4:21:57 AM12/22/25
to django-...@googlegroups.com
#36818: AttributeError: 'NoneType' object has no attribute 'getlimit' when calling
bulk_create as the first database operation on SQLite
-------------------------------------+-------------------------------------
Reporter: guro_ishiguro | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 6.0
(models, ORM) |
Severity: Normal | Resolution:
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
-------------------------------------+-------------------------------------
Changes (by Nilesh Pahari):

* cc: Nilesh Pahari (added)

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

Django

unread,
Dec 22, 2025, 7:33:29 AM12/22/25
to django-...@googlegroups.com
#36818: AttributeError: 'NoneType' object has no attribute 'getlimit' when calling
bulk_create as the first database operation on SQLite
-------------------------------------+-------------------------------------
Reporter: guro_ishiguro | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 6.0
(models, ORM) |
Severity: Normal | Resolution:
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
-------------------------------------+-------------------------------------
Comment (by Youngkwang Yang):

Related PR: https://github.com/django/django/pull/19427
--
Ticket URL: <https://code.djangoproject.com/ticket/36818#comment:2>

Django

unread,
Dec 22, 2025, 8:08:18 AM12/22/25
to django-...@googlegroups.com
#36818: AttributeError: 'NoneType' object has no attribute 'getlimit' when calling
bulk_create as the first database operation on SQLite
-------------------------------------+-------------------------------------
Reporter: guro_ishiguro | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 6.0
(models, ORM) |
Severity: Normal | Resolution:
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
-------------------------------------+-------------------------------------
Comment (by Youngkwang Yang):

I confirmed the issue with the following regression test:

{{{#!python

def test_max_query_params_without_established_connection(self):
new_connection = connection.copy()
new_connection.settings_dict =
copy.deepcopy(connection.settings_dict)
# Check if connection is not yet established
self.assertIsNone(new_connection.connection)
try:
result = new_connection.features.max_query_params
self.assertIsInstance(result, int)
finally:
new_connection.close()
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36818#comment:3>

Django

unread,
Dec 22, 2025, 9:18:15 AM12/22/25
to django-...@googlegroups.com
#36818: AttributeError: 'NoneType' object has no attribute 'getlimit' when calling
bulk_create as the first database operation on SQLite
-------------------------------------+-------------------------------------
Reporter: guro_ishiguro | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 6.0
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: sqlite bulk_create | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

* severity: Normal => Release blocker
* stage: Unreviewed => Accepted

Comment:

Confirmed regression in 358fd21c47cdf7bda520ce73c5cfd82bba57827b.

Would you like to prepare a patch? Using `ensure_connection` in
`max_query_params` seems like the way to go.
--
Ticket URL: <https://code.djangoproject.com/ticket/36818#comment:4>

Django

unread,
Dec 22, 2025, 9:28:55 AM12/22/25
to django-...@googlegroups.com
#36818: AttributeError: 'NoneType' object has no attribute 'getlimit' when calling
bulk_create as the first database operation on SQLite
-------------------------------------+-------------------------------------
Reporter: guro_ishiguro | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 6.0
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: sqlite bulk_create | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by guro_ishiguro):

Yes, I can prepare a patch. I'll take a look and post a PR.
--
Ticket URL: <https://code.djangoproject.com/ticket/36818#comment:5>

Django

unread,
Dec 22, 2025, 9:39:17 AM12/22/25
to django-...@googlegroups.com
#36818: AttributeError: 'NoneType' object has no attribute 'getlimit' when calling
bulk_create as the first database operation on SQLite
-------------------------------------+-------------------------------------
Reporter: guro_ishiguro | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 6.0
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: sqlite bulk_create | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Simon Charette):

Sounds good! Make sure to include
[https://github.com/django/django/blob/main/docs/releases/6.0.1.txt a
6.0.1 release note as well].
--
Ticket URL: <https://code.djangoproject.com/ticket/36818#comment:6>

Django

unread,
Dec 22, 2025, 9:56:48 AM12/22/25
to django-...@googlegroups.com
#36818: AttributeError: 'NoneType' object has no attribute 'getlimit' when calling
bulk_create as the first database operation on SQLite
-------------------------------------+-------------------------------------
Reporter: guro_ishiguro | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 6.0
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: sqlite bulk_create | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Varun Kasyap Pentamaraju):

maybe something like this helps


{{{
diff --git a/django/db/backends/sqlite3/features.py
b/django/db/backends/sqlite3/features.py
--- a/django/db/backends/sqlite3/features.py
+++ b/django/db/backends/sqlite3/features.py
@@ -157,6 +157,7 @@ class DatabaseFeatures(BaseDatabaseFeatures):
32766) or lowered per connection at run-time with
setlimit(SQLITE_LIMIT_VARIABLE_NUMBER, N).
"""
+ self.connection.ensure_connection()
return
self.connection.connection.getlimit(sqlite3.SQLITE_LIMIT_VARIABLE_NUMBER)

@cached_property
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36818#comment:7>

Django

unread,
Dec 22, 2025, 9:57:07 AM12/22/25
to django-...@googlegroups.com
#36818: AttributeError: 'NoneType' object has no attribute 'getlimit' when calling
bulk_create as the first database operation on SQLite
-------------------------------------+-------------------------------------
Reporter: guro_ishiguro | Owner: (none)
Type: Bug | Status: new
Component: Database layer | Version: 6.0
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: sqlite bulk_create | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Varun Kasyap Pentamaraju):

* cc: Varun Kasyap Pentamaraju (added)

--
Ticket URL: <https://code.djangoproject.com/ticket/36818#comment:8>

Django

unread,
Dec 22, 2025, 10:12:08 AM12/22/25
to django-...@googlegroups.com
#36818: AttributeError: 'NoneType' object has no attribute 'getlimit' when calling
bulk_create as the first database operation on SQLite
-------------------------------------+-------------------------------------
Reporter: guro_ishiguro | Owner:
| guro_ishiguro
Type: Bug | Status: assigned
Component: Database layer | Version: 6.0
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: sqlite bulk_create | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* owner: (none) => guro_ishiguro
* status: new => assigned

--
Ticket URL: <https://code.djangoproject.com/ticket/36818#comment:9>

Django

unread,
Dec 22, 2025, 11:00:33 AM12/22/25
to django-...@googlegroups.com
#36818: AttributeError: 'NoneType' object has no attribute 'getlimit' when calling
bulk_create as the first database operation on SQLite
-------------------------------------+-------------------------------------
Reporter: guro_ishiguro | Owner:
| guro_ishiguro
Type: Bug | Status: assigned
Component: Database layer | Version: 6.0
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: sqlite bulk_create | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by guro_ishiguro):

I’ve opened a pull request fixing this issue and added a regression test.

PR: https://github.com/django/django/pull/20443
--
Ticket URL: <https://code.djangoproject.com/ticket/36818#comment:10>

Django

unread,
Dec 22, 2025, 11:19:19 AM12/22/25
to django-...@googlegroups.com
#36818: AttributeError: 'NoneType' object has no attribute 'getlimit' when calling
bulk_create as the first database operation on SQLite
-------------------------------------+-------------------------------------
Reporter: guro_ishiguro | Owner:
| guro_ishiguro
Type: Bug | Status: assigned
Component: Database layer | Version: 6.0
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: sqlite bulk_create | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Varun Kasyap Pentamaraju):

* needs_better_patch: 0 => 1

--
Ticket URL: <https://code.djangoproject.com/ticket/36818#comment:11>

Django

unread,
Dec 22, 2025, 12:39:50 PM12/22/25
to django-...@googlegroups.com
#36818: AttributeError: 'NoneType' object has no attribute 'getlimit' when calling
bulk_create as the first database operation on SQLite
-------------------------------------+-------------------------------------
Reporter: guro_ishiguro | Owner:
| guro_ishiguro
Type: Bug | Status: assigned
Component: Database layer | Version: 6.0
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: sqlite bulk_create | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

* has_patch: 0 => 1
* needs_better_patch: 1 => 0
* stage: Accepted => Ready for checkin

--
Ticket URL: <https://code.djangoproject.com/ticket/36818#comment:12>

Django

unread,
Dec 22, 2025, 2:37:26 PM12/22/25
to django-...@googlegroups.com
#36818: AttributeError: 'NoneType' object has no attribute 'getlimit' when calling
bulk_create as the first database operation on SQLite
-------------------------------------+-------------------------------------
Reporter: guro_ishiguro | Owner:
| guro_ishiguro
Type: Bug | Status: assigned
Component: Database layer | Version: 6.0
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: sqlite bulk_create | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* needs_better_patch: 0 => 1
* stage: Ready for checkin => Accepted

--
Ticket URL: <https://code.djangoproject.com/ticket/36818#comment:13>

Django

unread,
Dec 22, 2025, 8:17:13 PM12/22/25
to django-...@googlegroups.com
#36818: AttributeError: 'NoneType' object has no attribute 'getlimit' when calling
bulk_create as the first database operation on SQLite
-------------------------------------+-------------------------------------
Reporter: guro_ishiguro | Owner:
| guro_ishiguro
Type: Bug | Status: assigned
Component: Database layer | Version: 6.0
(models, ORM) |
Severity: Release blocker | Resolution:
Keywords: sqlite bulk_create | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls):

* needs_better_patch: 1 => 0
* stage: Accepted => Ready for checkin

--
Ticket URL: <https://code.djangoproject.com/ticket/36818#comment:14>

Django

unread,
Dec 22, 2025, 8:33:22 PM12/22/25
to django-...@googlegroups.com
#36818: AttributeError: 'NoneType' object has no attribute 'getlimit' when calling
bulk_create as the first database operation on SQLite
-------------------------------------+-------------------------------------
Reporter: guro_ishiguro | Owner:
| guro_ishiguro
Type: Bug | Status: closed
Component: Database layer | Version: 6.0
(models, ORM) |
Severity: Release blocker | Resolution: fixed
Keywords: sqlite bulk_create | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Jacob Walls <jacobtylerwalls@…>):

* resolution: => fixed
* status: assigned => closed

Comment:

In [changeset:"84bae9c22a8ae7663c56cce5e0c611ea7c17fce1" 84bae9c]:
{{{#!CommitTicketReference repository=""
revision="84bae9c22a8ae7663c56cce5e0c611ea7c17fce1"
Fixed #36818 -- Ensured SQLite connection before accessing
max_query_params.

Regression in 358fd21c47cdf7bda520ce73c5cfd82bba57827b.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36818#comment:15>

Django

unread,
Dec 22, 2025, 8:34:15 PM12/22/25
to django-...@googlegroups.com
#36818: AttributeError: 'NoneType' object has no attribute 'getlimit' when calling
bulk_create as the first database operation on SQLite
-------------------------------------+-------------------------------------
Reporter: guro_ishiguro | Owner:
| guro_ishiguro
Type: Bug | Status: closed
Component: Database layer | Version: 6.0
(models, ORM) |
Severity: Release blocker | Resolution: fixed
Keywords: sqlite bulk_create | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Jacob Walls <jacobtylerwalls@…>):

In [changeset:"847b2badccbd87fc3e657725eb316b092265912d" 847b2bad]:
{{{#!CommitTicketReference repository=""
revision="847b2badccbd87fc3e657725eb316b092265912d"
[6.0.x] Fixed #36818 -- Ensured SQLite connection before accessing
max_query_params.

Regression in 358fd21c47cdf7bda520ce73c5cfd82bba57827b.

Backport of 84bae9c22a8ae7663c56cce5e0c611ea7c17fce1 from main.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/36818#comment:16>
Reply all
Reply to author
Forward
0 new messages