summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2013-10-12 20:21:18 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2013-10-12 20:21:18 -0400
commita2cce1bf43552e699f2babe7e4750354f2d580fe (patch)
tree6a51ca1dba5b26cd69770f777ba89c429cafb177
parent9bc9d5c1068be878118202259add3c2e1bcec0cb (diff)
downloadsqlalchemy-a2cce1bf43552e699f2babe7e4750354f2d580fe.tar.gz
Parenthesis will be applied to a compound SQL expression as
rendered in the column list of a CREATE INDEX statement. [ticket:2742]
-rw-r--r--doc/build/changelog/changelog_08.rst8
-rw-r--r--lib/sqlalchemy/dialects/postgresql/base.py5
-rw-r--r--test/dialect/postgresql/test_compiler.py13
3 files changed, 24 insertions, 2 deletions
diff --git a/doc/build/changelog/changelog_08.rst b/doc/build/changelog/changelog_08.rst
index 8de572b20..5892262b4 100644
--- a/doc/build/changelog/changelog_08.rst
+++ b/doc/build/changelog/changelog_08.rst
@@ -11,6 +11,14 @@
: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
:versions: 0.9.0
diff --git a/lib/sqlalchemy/dialects/postgresql/base.py b/lib/sqlalchemy/dialects/postgresql/base.py
index 83ad1d46f..06ee8c3a2 100644
--- a/lib/sqlalchemy/dialects/postgresql/base.py
+++ b/lib/sqlalchemy/dialects/postgresql/base.py
@@ -1101,7 +1101,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 858f3e763..76fd9d907 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):