[Django] #33160: postgresql DatabaseWrapper._nodb_cursor causes confusing/wrong "Django was unable to create a connection to the 'postgres' database" warning in case of errors from queries

35 views
Skip to first unread message

Django

unread,
Sep 30, 2021, 9:45:55 AM9/30/21
to django-...@googlegroups.com
#33160: postgresql DatabaseWrapper._nodb_cursor causes confusing/wrong "Django was
unable to create a connection to the 'postgres' database" warning in case
of errors from queries
-------------------------------------+-------------------------------------
Reporter: Daniel | Owner: nobody
Hahler |
Type: Bug | Status: new
Component: Database | Version: 3.2
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
{{_nodb_cursor}} might cause a confusing/wrong warning, e.g. via
{{teardown_database}}:

{{{
[35] …/Vcs/django/django/test/utils.py(313)teardown_databases()
connection.creation.destroy_test_db(old_name, verbosity, keepdb)
[36]
…/Vcs/django/django/db/backends/base/creation.py(282)destroy_test_db()
self._destroy_test_db(test_database_name, verbosity)
[37]
…/Vcs/django/django/db/backends/base/creation.py(298)_destroy_test_db()
cursor.execute("DROP DATABASE %s"
[38] /usr/lib/python3.9/contextlib.py(137)__exit__()
self.gen.throw(typ, value, traceback)
> [39]
…/Vcs/django/django/db/backends/postgresql/base.py(306)_nodb_cursor()
warnings.warn(
(Pdb++) l
301 with super()._nodb_cursor() as cursor:
302 yield cursor
303 except (Database.DatabaseError, WrappedDatabaseError) as
exc:
304 e = exc
305 __import__('pdb').set_trace()
306 -> warnings.warn(
307 "Normally Django will use a connection to the
'postgres' database "
308 "to avoid running initialization queries against
the production "
309 "database when it's not needed (for example, when
running tests). "
310 "Django was unable to create a connection to the
'postgres' database "
311 "and will use the first PostgreSQL database
instead.",
(Pdb++) e
OperationalError('database "test_foo" is being accessed by other
users\nDETAIL: There is 1 other session using the database.\n')
}}}

As you can see there is another issue this will (partly) swallow/hide
then, apart from the message being just wrong: it is not the connection
that failed, but a query in there:

{{{
[37] >
…/django/django/db/backends/base/creation.py(298)_destroy_test_db(), 4
frames hidden

289 def _destroy_test_db(self, test_database_name, verbosity):
290 """
291 Internal implementation - remove the test db tables.
292 """
293 # Remove the test database to clean up after
294 # ourselves. Connect to the previous database (not the
test database)
295 # to do so, because it's not allowed to delete a database
while being
296 # connected to it.
297 with self._nodb_cursor() as cursor:
298 -> cursor.execute("DROP DATABASE %s"
299 %
self.connection.ops.quote_name(test_database_name))
}}}

Only {{connect()} should get wrapped/handled in {{_nodb_cursor}} probably
(in terms of adding the warning), which could be achieved by handling
specific exceptions only, or re-raising it in case a {{cursor}} was used
(i.e. it could connect).

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

Django

unread,
Sep 30, 2021, 9:52:26 AM9/30/21
to django-...@googlegroups.com
#33160: postgresql DatabaseWrapper._nodb_cursor causes confusing/wrong "Django was
unable to create a connection to the 'postgres' database" warning in case
of errors from queries
-------------------------------------+-------------------------------------
Reporter: Daniel Hahler | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Daniel Hahler):

* has_patch: 0 => 1


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

Django

unread,
Sep 30, 2021, 9:52:42 AM9/30/21
to django-...@googlegroups.com
#33160: postgresql DatabaseWrapper._nodb_cursor causes confusing/wrong "Django was
unable to create a connection to the 'postgres' database" warning in case
of errors from queries
-------------------------------------+-------------------------------------
Reporter: Daniel Hahler | Owner: nobody
Type: Bug | Status: new

Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Daniel Hahler:

Old description:

New description:

Possible fix: https://github.com/django/django/pull/14918

--

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

Django

unread,
Sep 30, 2021, 2:33:39 PM9/30/21
to django-...@googlegroups.com
#33160: DatabaseWrapper._nodb_cursor() raises wrong warnings in case of errors from
queries on PostgreSQL.
-------------------------------------+-------------------------------------
Reporter: Daniel Hahler | Owner: Daniel
| Hahler
Type: Bug | Status: assigned

Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* owner: nobody => Daniel Hahler
* status: new => assigned
* needs_tests: 0 => 1
* stage: Unreviewed => Accepted


Comment:

Thanks for the report.

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

Django

unread,
Sep 30, 2021, 3:46:19 PM9/30/21
to django-...@googlegroups.com
#33160: DatabaseWrapper._nodb_cursor() raises wrong warnings in case of errors from
queries on PostgreSQL.
-------------------------------------+-------------------------------------
Reporter: Daniel Hahler | Owner: Daniel
| Hahler
Type: Bug | Status: assigned
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Daniel Hahler):

* needs_tests: 1 => 0


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

Django

unread,
Oct 1, 2021, 1:24:09 AM10/1/21
to django-...@googlegroups.com
#33160: DatabaseWrapper._nodb_cursor() raises wrong warnings in case of errors from
queries on PostgreSQL.
-------------------------------------+-------------------------------------
Reporter: Daniel Hahler | Owner: Daniel
| Hahler
Type: Bug | Status: closed

Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by GitHub <noreply@…>):

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


Comment:

In [changeset:"98c8bf1ceeab5c68751c83555f82cff1a9120a67" 98c8bf1]:
{{{
#!CommitTicketReference repository=""
revision="98c8bf1ceeab5c68751c83555f82cff1a9120a67"
Fixed #33160 -- Avoided suppressing query errors in _nodb_cursor() on
PostgreSQL.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/33160#comment:5>

Django

unread,
Oct 1, 2021, 1:25:19 AM10/1/21
to django-...@googlegroups.com
#33160: DatabaseWrapper._nodb_cursor() raises wrong warnings in case of errors from
queries on PostgreSQL.
-------------------------------------+-------------------------------------
Reporter: Daniel Hahler | Owner: Daniel
| Hahler
Type: Bug | Status: closed
Component: Database layer | Version: 3.2
(models, ORM) |
Severity: Normal | Resolution: fixed
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"81bb0ae2213019726a96becb5a1d906924dd5109" 81bb0ae2]:
{{{
#!CommitTicketReference repository=""
revision="81bb0ae2213019726a96becb5a1d906924dd5109"
[4.0.x] Fixed #33160 -- Avoided suppressing query errors in _nodb_cursor()
on PostgreSQL.

Backport of 98c8bf1ceeab5c68751c83555f82cff1a9120a67 from main
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/33160#comment:6>

Reply all
Reply to author
Forward
0 new messages