It seems to be because the instance of DatabaseWrapper passed in to as_sql
doesn't have a connection: DatabaseOperations.compose_sql calls mogrify,
which in turn calls .connection on the DatabaseWrapper, which returns
NoneType.
I've verified that calling connection.connect() in search.py immediately
before the compose_sql line resolves this, but I don't know nearly enough
to know if this is the real root problem, or where in the stack the
connection is supposed to be created if it is.
--
Ticket URL: <https://code.djangoproject.com/ticket/34486>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* Attachment "traceback.txt" added.
Traceback
* type: Uncategorized => Bug
Comment:
I guess it's fixed by 4bf4222010fd8e413963c6c873e4088614332ef9 which
removed `compose_sql()`?
--
Ticket URL: <https://code.djangoproject.com/ticket/34486#comment:1>
Comment (by Scott Macpherson):
That commit does indeed resolve this issue. While checking that out I
discovered that SearchHeadline.as_sql suffers from the same issue. Should
I log a separate bug for that?
--
Ticket URL: <https://code.djangoproject.com/ticket/34486#comment:2>
* status: new => closed
* resolution: => duplicate
Comment:
Replying to [comment:2 Scott Macpherson]:
> While checking that out I discovered that SearchHeadline.as_sql suffers
from the same issue. Should I log a separate bug for that?
Does it? In `SearchHeadline.as_sql()` composed options are passed in
params, so it should not be affected.
Duplicate of #34459.
--
Ticket URL: <https://code.djangoproject.com/ticket/34486#comment:3>
Comment (by Scott Macpherson):
The trouble doesn't seem to be with the structure of the call to
`compose_sql`, but it's called on a `DatabaseOperations` object with a
`DatabaseWrapper` that doesn't have a connection. When `mogrify` retrieves
that connection it gets a NoneType back.
I can't work out why the tests in `SearchHeadlineTests` don't capture this
- could it have something to do with the way connections are reused in the
tests, but not for a cold request?
{{{
…
File "/Users/scott/Development/python_venvs/zerosleeps/lib/python3.11
/site-packages/django/contrib/postgres/search.py", line 320, in as_sql
", ".join(
^
File "/Users/scott/Development/python_venvs/zerosleeps/lib/python3.11
/site-packages/django/contrib/postgres/search.py", line 321, in <genexpr>
connection.ops.compose_sql(f"{option}=%s", [value])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/scott/Development/python_venvs/zerosleeps/lib/python3.11
/site-packages/django/db/backends/postgresql/operations.py", line 205, in
compose_sql
return mogrify(sql, params, self.connection)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/scott/Development/python_venvs/zerosleeps/lib/python3.11
/site-packages/django/db/backends/postgresql/psycopg_any.py", line 21, in
mogrify
return ClientCursor(connection.connection).mogrify(sql, params)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/scott/Development/python_venvs/zerosleeps/lib/python3.11
/site-packages/psycopg/cursor.py", line 672, in __init__
super().__init__(connection)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/Users/scott/Development/python_venvs/zerosleeps/lib/python3.11
/site-packages/psycopg/cursor.py", line 65, in __init__
self._pgconn = connection.pgconn
^^^^^^^^^^^^^^^^^
Exception Type: AttributeError at /search/
Exception Value: 'NoneType' object has no attribute 'pgconn'
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/34486#comment:4>
Comment (by Mariusz Felisiak):
Replying to [comment:4 Scott Macpherson]:
> I can't work out why the tests in `SearchHeadlineTests` don't capture
this - could it have something to do with the way connections are reused
in the tests, but not for a cold request?
I'm not sure how it is possible that you reach this point without a
connection. Can you provide a regression test or a small project that
reproduces the issue?
--
Ticket URL: <https://code.djangoproject.com/ticket/34486#comment:5>
Comment (by Scott Macpherson):
I've knocked up a little project which demonstrates the issue:
https://github.com/scottmacpherson/postgres_search
--
Ticket URL: <https://code.djangoproject.com/ticket/34486#comment:6>