Skip to content

Commit b918ab9

Browse files
committed
SA14/Tests: Declare column expressions for _score as literal_column
With the original code, SA14 would croak like:: sqlalchemy.exc.ArgumentError: Textual column expression '_score' should be explicitly declared with text('_score'), or use column('_score') for more specificity However, when using the `sa.text` type, as suggested, older SQLAlchemy versions will croak in this manner:: sqlalchemy.exc.InvalidRequestError: SQL expression, column, or mapped entity expected - got '<sqlalchemy.sql.elements.TextClause object at 0x10c0f7460>' The `sa.column` type also will not work, because it would get rendered with quotes as `"_score"`. > `literal_column()` is similar to `column()`, except that it is more > often used as a “standalone” column expression that renders exactly > as stated. > > -- https://docs.sqlalchemy.org/en/14/core/sqlelement.html#sqlalchemy.sql.expression.literal_column :: - SELECT characters.name AS characters_name, _score FROM characters WHERE match(characters.name, ?) + SELECT characters.name AS characters_name, "_score" FROM characters WHERE match(characters.name, ?) Settling with the `sa.literal_column` type resolved any woes.
1 parent bd5a806 commit b918ab9

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

CHANGES.txt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,40 @@ Unreleased
2424

2525
- Add support for SQLAlchemy 1.4
2626

27+
28+
Breaking changes
29+
----------------
30+
31+
Textual column expressions
32+
''''''''''''''''''''''''''
33+
34+
On the update to SQLAlchemy 1.4, some test cases had to be adjusted in order
35+
to compensate for apparent additional strictness of SQLAlchemy on some details.
36+
37+
Where it was ok to use a textual column expression in plain text beforehand,
38+
a `SQLAlchemy literal_column`_ type should be used now. Otherwise, for example
39+
when accessing `CrateDB system columns`_ like ``_score``, the engine might
40+
complain like::
41+
42+
sqlalchemy.exc.ArgumentError: Textual column expression '_score' should be
43+
explicitly declared with text('_score'), or use column('_score') for more
44+
specificity
45+
46+
The changes to be made look like this::
47+
48+
old: session.query(Character.name, '_score')
49+
new: session.query(Character.name, sa.literal_column('_score'))
50+
51+
::
52+
53+
old: .order_by(sa.desc(sa.text('_score')))
54+
new: .order_by(sa.desc(sa.literal_column('_score')))
55+
56+
57+
.. _SQLAlchemy literal_column: https://docs.sqlalchemy.org/en/14/core/sqlelement.html#sqlalchemy.sql.expression.literal_column
58+
.. _CrateDB system columns: https://crate.io/docs/crate/reference/en/4.8/general/ddl/system-columns.html
59+
60+
2761
2020/09/28 0.26.0
2862
=================
2963

src/crate/client/doctests/sqlalchemy.txt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ the other rows. The higher the score value, the more relevant the row.
289289
In most cases ``_score`` is not part of the SQLAlchemy Table definition,
290290
so it must be passed as a string::
291291

292-
>>> session.query(Character.name, '_score') \
292+
>>> session.query(Character.name, sa.literal_column('_score')) \
293293
... .filter(match(Character.quote_ft, 'space')) \
294294
... .all()
295295
[('Tricia McMillan', ...)]
@@ -298,11 +298,10 @@ To search on multiple columns you have to pass a dictionary with columns
298298
and ``boost`` attached. ``boost`` is a factor that increases the
299299
relevance of a column in respect to the other columns::
300300

301-
>>> from sqlalchemy.sql import text
302301
>>> session.query(Character.name) \
303302
... .filter(match({Character.name_ft: 1.5, Character.quote_ft: 0.1},
304303
... 'Arthur')) \
305-
... .order_by(sa.desc(text('_score'))) \
304+
... .order_by(sa.desc(sa.literal_column('_score'))) \
306305
... .all()
307306
[('Arthur Dent',), ('Tricia McMillan',)]
308307

src/crate/client/sqlalchemy/tests/match_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ def test_match_type_options(self):
109109
)
110110

111111
def test_score(self):
112-
query = self.session.query(self.Character.name, '_score') \
112+
query = self.session.query(self.Character.name,
113+
sa.literal_column('_score')) \
113114
.filter(match(self.Character.name, 'Trillian'))
114115
self.assertSQL(
115116
"SELECT characters.name AS characters_name, _score " +

0 commit comments

Comments
 (0)