diff options
| author | RamonWill <ramonwilliams@hotmail.co.uk> | 2020-09-16 17:47:21 -0400 |
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2020-10-12 17:38:34 -0400 |
| commit | 1cda8e18bf4e9b36d07b9d54c04e1fe035977064 (patch) | |
| tree | 9d7ad74137f096b2c105ccbf7932529d3ad2c83c /lib/sqlalchemy/dialects/mysql | |
| parent | 3fcdb7cca05de3a90c4f3b9f96ae680618a26c41 (diff) | |
| download | sqlalchemy-1cda8e18bf4e9b36d07b9d54c04e1fe035977064.tar.gz | |
Support indexing on expressions and functions for the MySQL dialect
A user noticed that creating an index where the "key part" was an expression
or function would raise an error for MySQL because the key part was not
parenthesized. The proposed change will check whether a key part is not a
Column or Unary Expression and parenthesize if the case is False.
This fix also contains a minor fix to a test case that was previously incorrect
(`def test_create_index_expr():`).
**Have a nice day!**
Fixes: #5462
Closes: #5587
Pull-request: https://github.com/sqlalchemy/sqlalchemy/pull/5587
Pull-request-sha: 7515e50cd7435744fc79c210b2f3aa4c0546ba28
Change-Id: Id1b3b3026983c0e05808baa243e354f82b78180c
Diffstat (limited to 'lib/sqlalchemy/dialects/mysql')
| -rw-r--r-- | lib/sqlalchemy/dialects/mysql/base.py | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/lib/sqlalchemy/dialects/mysql/base.py b/lib/sqlalchemy/dialects/mysql/base.py index 8fb4c3b4b..3e6c676ab 100644 --- a/lib/sqlalchemy/dialects/mysql/base.py +++ b/lib/sqlalchemy/dialects/mysql/base.py @@ -952,6 +952,7 @@ from ...engine import reflection from ...sql import coercions from ...sql import compiler from ...sql import elements +from ...sql import operators from ...sql import roles from ...sql import util as sql_util from ...sql.sqltypes import Unicode @@ -1975,9 +1976,21 @@ class MySQLDDLCompiler(compiler.DDLCompiler): self._verify_index_table(index) preparer = self.preparer table = preparer.format_table(index.table) + columns = [ self.sql_compiler.process( - expr, include_table=False, literal_binds=True + elements.Grouping(expr) + if ( + not isinstance(expr, elements.ColumnClause) + and ( + not isinstance(expr, elements.UnaryExpression) + or expr.modifier + not in (operators.desc_op, operators.asc_op) + ) + ) + else expr, + include_table=False, + literal_binds=True, ) for expr in index.expressions ] |
