[Django] #34486: SearchVector.as_sql causes AttributeError

7 views
Skip to first unread message

Django

unread,
Apr 11, 2023, 10:20:55 PM4/11/23
to django-...@googlegroups.com
#34486: SearchVector.as_sql causes AttributeError
--------------------------------------------+------------------------
Reporter: Scott Macpherson | Owner: (none)
Type: Uncategorized | Status: new
Component: contrib.postgres | Version: 4.2
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
--------------------------------------------+------------------------
Following an upgrade to Django 4.2 and psycopg3, any use of SearchVector
has started returning AttributeError "'NoneType' object has no attribute
'pgconn'".

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.

Django

unread,
Apr 11, 2023, 10:21:22 PM4/11/23
to django-...@googlegroups.com
#34486: SearchVector.as_sql causes AttributeError
----------------------------------+--------------------------------------

Reporter: Scott Macpherson | Owner: (none)
Type: Uncategorized | Status: new
Component: contrib.postgres | Version: 4.2
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 Scott Macpherson):

* Attachment "traceback.txt" added.

Traceback

Django

unread,
Apr 11, 2023, 11:32:36 PM4/11/23
to django-...@googlegroups.com
#34486: SearchVector.as_sql causes AttributeError
----------------------------------+--------------------------------------
Reporter: Scott Macpherson | Owner: (none)
Type: Bug | Status: new

Component: contrib.postgres | Version: 4.2
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 Tim Graham):

* 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>

Django

unread,
Apr 12, 2023, 12:54:35 AM4/12/23
to django-...@googlegroups.com
#34486: SearchVector.as_sql causes AttributeError
----------------------------------+--------------------------------------
Reporter: Scott Macpherson | Owner: (none)
Type: Bug | Status: new
Component: contrib.postgres | Version: 4.2
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
----------------------------------+--------------------------------------

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>

Django

unread,
Apr 12, 2023, 2:15:37 AM4/12/23
to django-...@googlegroups.com
#34486: SearchVector.as_sql causes AttributeError
----------------------------------+--------------------------------------
Reporter: Scott Macpherson | Owner: (none)
Type: Bug | Status: closed
Component: contrib.postgres | Version: 4.2
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:

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>

Django

unread,
Apr 12, 2023, 3:36:32 AM4/12/23
to django-...@googlegroups.com
#34486: SearchVector.as_sql causes AttributeError
----------------------------------+--------------------------------------
Reporter: Scott Macpherson | Owner: (none)
Type: Bug | Status: closed
Component: contrib.postgres | Version: 4.2
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 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>

Django

unread,
Apr 12, 2023, 3:52:11 AM4/12/23
to django-...@googlegroups.com
#34486: SearchVector.as_sql causes AttributeError
----------------------------------+--------------------------------------
Reporter: Scott Macpherson | Owner: (none)
Type: Bug | Status: closed
Component: contrib.postgres | Version: 4.2
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 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>

Django

unread,
Apr 12, 2023, 5:12:46 AM4/12/23
to django-...@googlegroups.com
#34486: SearchVector.as_sql causes AttributeError
----------------------------------+--------------------------------------
Reporter: Scott Macpherson | Owner: (none)
Type: Bug | Status: closed
Component: contrib.postgres | Version: 4.2
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 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>

Reply all
Reply to author
Forward
0 new messages