From: Anton Kovalevich Date: Tue, 5 Oct 2021 22:16:02 +0000 (-0400) Subject: Bugfix: MySQL expression: may be bindparam or other expression X-Git-Tag: rel_1_4_26~33^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=5177f380c8be607c85fa23657f2e1cec0eabb24b;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Bugfix: MySQL expression: may be bindparam or other expression Fixed issue in MySQL :func:`_mysql.match` construct where passing a clause expression such as :func:`_sql.bindparam` or other SQL expression for the "against" parameter would fail. Pull request courtesy Anton Kovalevich. Fixes: #7144 Closes: #7145 Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/7145 Pull-request-sha: 3757ffa51a3ff5919278165aaf906c8d2f9940c0 Change-Id: I7bb95f338afe3a17296a7b60e8c973f93b4f5e62 --- diff --git a/doc/build/changelog/unreleased_14/7144.rst b/doc/build/changelog/unreleased_14/7144.rst new file mode 100644 index 0000000000..fee4edefd7 --- /dev/null +++ b/doc/build/changelog/unreleased_14/7144.rst @@ -0,0 +1,8 @@ +.. change:: + :tags: bug, mysql + :tickets: 7144 + + Fixed issue in MySQL :func:`_mysql.match` construct where passing a clause + expression such as :func:`_sql.bindparam` or other SQL expression for the + "against" parameter would fail. Pull request courtesy Anton Kovalevich. + diff --git a/lib/sqlalchemy/dialects/mysql/expression.py b/lib/sqlalchemy/dialects/mysql/expression.py index d6ef80ef6b..7a66e9b142 100644 --- a/lib/sqlalchemy/dialects/mysql/expression.py +++ b/lib/sqlalchemy/dialects/mysql/expression.py @@ -68,7 +68,7 @@ class match(Generative, elements.BinaryExpression): against = kw.pop("against", None) - if not against: + if against is None: raise exc.ArgumentError("against is required") against = coercions.expect( roles.ExpressionElementRole, diff --git a/test/dialect/mysql/test_compiler.py b/test/dialect/mysql/test_compiler.py index 169d46ed24..b43f7de711 100644 --- a/test/dialect/mysql/test_compiler.py +++ b/test/dialect/mysql/test_compiler.py @@ -53,6 +53,7 @@ from sqlalchemy.dialects.mysql import insert from sqlalchemy.dialects.mysql import match from sqlalchemy.sql import column from sqlalchemy.sql import table +from sqlalchemy.sql.expression import bindparam from sqlalchemy.sql.expression import literal_column from sqlalchemy.testing import assert_raises_message from sqlalchemy.testing import AssertsCompiledSQL @@ -1283,6 +1284,26 @@ class MatchExpressionTest(fixtures.TestBase, AssertsCompiledSQL): expr = case(expr) self.assert_compile(expr, expected) + @testing.combinations( + (bindparam("against_expr"), "%s"), + ( + column("some col") + column("some other col"), + "`some col` + `some other col`", + ), + (column("some col") + bindparam("against_expr"), "`some col` + %s"), + ) + def test_match_expression_against_expr(self, against, expected_segment): + firstname = self.match_table.c.firstname + lastname = self.match_table.c.lastname + + expr = match(firstname, lastname, against=against) + + expected = ( + "MATCH (user.firstname, user.lastname) AGAINST (%s)" + % expected_segment + ) + self.assert_compile(expr, expected) + def test_cols_required(self): assert_raises_message( exc.ArgumentError,