[Django] #33153: "database table is locked" error on SQLite when testing with mirror database

150 views
Skip to first unread message

Django

unread,
Sep 28, 2021, 1:28:21 PM9/28/21
to django-...@googlegroups.com
#33153: "database table is locked" error on SQLite when testing with mirror
database
---------------------------------------------+------------------------
Reporter: Adam Johnson | Owner: nobody
Type: Bug | Status: new
Component: Testing framework | Version: 4.0
Severity: Release blocker | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
---------------------------------------------+------------------------
Requirements:
* project uses SQLite for database
* has a "replica" db alias set up as test mirror of default
* `TestCase` has `databases = ["default", "replica"]`
* Model is either from or two in a FK relationship
* `TestCase` tries to create an instance of that model

Result: The SQLite foreign key check, `PRAGMA foreign_key_check`, fails
with an `OperationalError` like:

{{{
Traceback (most recent call last):
File "/.../site-packages/django/test/testcases.py", line 278, in
_setup_and_call
self._post_teardown()
File "/.../site-packages/django/test/testcases.py", line 1023, in
_post_teardown
self._fixture_teardown()
File "/.../site-packages/django/test/testcases.py", line 1266, in
_fixture_teardown
connections[db_name].check_constraints()
File "/.../site-packages/django/db/backends/sqlite3/base.py", line 322,
in check_constraints
violations = cursor.execute('PRAGMA foreign_key_check').fetchall()
File "/.../site-packages/django/db/backends/utils.py", line 66, in
execute
return self._execute_with_wrappers(sql, params, many=False,
executor=self._execute)
File "/.../site-packages/django/db/backends/utils.py", line 75, in
_execute_with_wrappers
return executor(sql, params, many, context)
File "/.../site-packages/django/db/backends/utils.py", line 84, in
_execute
return self.cursor.execute(sql, params)
File "/.../site-packages/django/db/utils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/.../site-packages/django/db/backends/utils.py", line 82, in
_execute
return self.cursor.execute(sql)
File "/.../site-packages/django/db/backends/sqlite3/base.py", line 414,
in execute
return Database.Cursor.execute(self, query)
django.db.utils.OperationalError: database table is locked: core_author
}}}

Reproduction project to follow.

--
Ticket URL: <https://code.djangoproject.com/ticket/33153>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Sep 28, 2021, 1:30:43 PM9/28/21
to django-...@googlegroups.com
#33153: "database table is locked" error on SQLite when testing with mirror
database
-----------------------------------+--------------------------------------

Reporter: Adam Johnson | Owner: nobody
Type: Bug | Status: new
Component: Testing framework | Version: 4.0
Severity: Release blocker | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------
Description changed by Adam Johnson:

Old description:

New description:

Reproduction project: https://github.com/adamchainz/django-ticket-33153 .
Simply run `./manage.py test` to see the above exception.

Removing the FK from the model, the `"replica"` entry in
`AuthorTests.databases`, or the mirror declaration in
`settings.py:DATABASES` causes the test to pass.

--

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

Django

unread,
Sep 28, 2021, 1:39:50 PM9/28/21
to django-...@googlegroups.com
#33153: "database table is locked" error on SQLite when testing with mirror
database
-----------------------------------+--------------------------------------

Reporter: Adam Johnson | Owner: nobody
Type: Bug | Status: new
Component: Testing framework | Version: dev
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------
Changes (by Adam Johnson):

* version: 4.0 => dev
* severity: Release blocker => Normal


Comment:

Okay so this actually reproduces back to Django 2.2 with the example repo.
That makes sense since it was when end-of-test checking of deferrable
database constraints was added for SQLite.

It looked like a Django 4.0 specific issue to me because it only started
occurring for me when adding Django 4.0 support to django-perf-rec
([https://github.com/adamchainz/django-perf-rec/pull/406 PR]). I will try
work out why it hasn't failed on 2.2 onwards....

--
Ticket URL: <https://code.djangoproject.com/ticket/33153#comment:2>

Django

unread,
Sep 28, 2021, 2:31:48 PM9/28/21
to django-...@googlegroups.com
#33153: "database table is locked" error on SQLite when testing with mirror
database
-----------------------------------+--------------------------------------

Reporter: Adam Johnson | Owner: nobody
Type: Bug | Status: closed

Component: Testing framework | Version: dev
Severity: Normal | Resolution: duplicate

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------
Changes (by Mariusz Felisiak):

* status: new => closed
* resolution: => duplicate


Comment:

I think it's a duplicate of #23718. `TEST['MIRROR']` requires
`TransactionTestCase` to work with multiple databases.

--
Ticket URL: <https://code.djangoproject.com/ticket/33153#comment:3>

Django

unread,
Sep 28, 2021, 2:37:17 PM9/28/21
to django-...@googlegroups.com
#33153: "database table is locked" error on SQLite when testing with mirror
database
-----------------------------------+--------------------------------------

Reporter: Adam Johnson | Owner: nobody
Type: Bug | Status: closed
Component: Testing framework | Version: dev
Severity: Normal | Resolution: duplicate
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+--------------------------------------

Comment (by Adam Johnson):

Quite possibly! The project is fixed by using `TransactionTestCase`.

--
Ticket URL: <https://code.djangoproject.com/ticket/33153#comment:4>

Reply all
Reply to author
Forward
0 new messages