From: Mike Bayer Date: Sun, 13 Oct 2013 00:21:18 +0000 (-0400) Subject: Parenthesis will be applied to a compound SQL expression as X-Git-Tag: rel_0_8_3~16 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=70fdd3efa7a9a9a0fcf6cebf0853c44f04cf9a04;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Parenthesis will be applied to a compound SQL expression as rendered in the column list of a CREATE INDEX statement. [ticket:2742] --- diff --git a/doc/build/changelog/changelog_08.rst b/doc/build/changelog/changelog_08.rst index 3d2179f5a7..134a683493 100644 --- a/doc/build/changelog/changelog_08.rst +++ b/doc/build/changelog/changelog_08.rst @@ -10,6 +10,14 @@ .. changelog:: :version: 0.8.3 + .. change:: + :tags: bug, postgresql + :tickets: 2742 + :versions: 0.9.0 + + Parenthesis will be applied to a compound SQL expression as + rendered in the column list of a CREATE INDEX statement. + .. change:: :tags: bug, sql :tickets: 2742 diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py index 2cbe7c8300..b879fb742b 100644 --- a/lib/sqlalchemy/dialects/postgresql/base.py +++ b/lib/sqlalchemy/dialects/postgresql/base.py @@ -1102,7 +1102,10 @@ class PGDDLCompiler(compiler.DDLCompiler): % ( ', '.join([ self.sql_compiler.process( - expr, include_table=False, literal_binds=True) + + expr.self_group() + if not isinstance(expr, expression.ColumnClause) + else expr, + include_table=False, literal_binds=True) + (c.key in ops and (' ' + ops[c.key]) or '') for expr, c in zip(index.expressions, index.columns)]) ) diff --git a/test/dialect/postgresql/test_compiler.py b/test/dialect/postgresql/test_compiler.py index 858f3e7636..76fd9d9072 100644 --- a/test/dialect/postgresql/test_compiler.py +++ b/test/dialect/postgresql/test_compiler.py @@ -173,6 +173,17 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL): 'USING hash (data)', dialect=postgresql.dialect()) + + def test_create_index_expr_gets_parens(self): + m = MetaData() + tbl = Table('testtbl', m, Column('x', Integer), Column('y', Integer)) + + idx1 = Index('test_idx1', 5 / (tbl.c.x + tbl.c.y)) + self.assert_compile( + schema.CreateIndex(idx1), + "CREATE INDEX test_idx1 ON testtbl ((5 / (x + y)))" + ) + def test_create_index_literals(self): m = MetaData() tbl = Table('testtbl', m, Column('data', Integer)) @@ -180,7 +191,7 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL): idx1 = Index('test_idx1', tbl.c.data + 5) self.assert_compile( schema.CreateIndex(idx1), - "CREATE INDEX test_idx1 ON testtbl (data + 5)" + "CREATE INDEX test_idx1 ON testtbl ((data + 5))" ) def test_exclude_constraint_min(self):