summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGES2
-rw-r--r--lib/sqlalchemy/ansisql.py6
-rw-r--r--test/sql/labels.py14
3 files changed, 19 insertions, 3 deletions
diff --git a/CHANGES b/CHANGES
index 64f59d05c..193025ab2 100644
--- a/CHANGES
+++ b/CHANGES
@@ -2,6 +2,8 @@
- _Label class overrides compare_self to return its ultimate object.
meaning, if you say someexpr.label('foo') == 5, it produces
the correct "someexpr == 5".
+ - fix to long name generation when using oid_column as an order by
+ (oids used heavily in mapper queries)
- orm
- "delete-orphan" no longer implies "delete". ongoing effort to
separate the behavior of these two operations.
diff --git a/lib/sqlalchemy/ansisql.py b/lib/sqlalchemy/ansisql.py
index cd6e0858f..ab043f3ec 100644
--- a/lib/sqlalchemy/ansisql.py
+++ b/lib/sqlalchemy/ansisql.py
@@ -265,7 +265,7 @@ class ANSICompiler(sql.Compiled):
name = self._truncated_identifier("colident", column.name)
else:
name = column.name
-
+
if column.table is None or not column.table.named_with_column():
self.strings[column] = self.preparer.format_column(column, name=name)
else:
@@ -274,7 +274,9 @@ class ANSICompiler(sql.Compiled):
if n is not None:
self.strings[column] = "%s.%s" % (self.preparer.format_table(column.table, use_schema=False), n)
elif len(column.table.primary_key) != 0:
- self.strings[column] = self.preparer.format_column_with_table(list(column.table.primary_key)[0])
+ pk = list(column.table.primary_key)[0]
+ pkname = (pk.is_literal and name or self._truncated_identifier("colident", pk.name))
+ self.strings[column] = self.preparer.format_column_with_table(list(column.table.primary_key)[0], column_name=pkname)
else:
self.strings[column] = None
else:
diff --git a/test/sql/labels.py b/test/sql/labels.py
index 7d458da06..ee9fa6bc5 100644
--- a/test/sql/labels.py
+++ b/test/sql/labels.py
@@ -22,6 +22,7 @@ class LongLabelsTest(testbase.PersistTest):
Column("this_is_the_primarykey_column", Integer, Sequence("this_is_some_large_seq"), primary_key=True),
Column("this_is_the_data_column", String(30))
)
+
metadata.create_all()
def tearDown(self):
table1.delete().execute()
@@ -77,6 +78,17 @@ class LongLabelsTest(testbase.PersistTest):
q = table1.select(table1.c.this_is_the_primarykey_column == 4).alias('foo')
x = select([q])
print x.execute().fetchall()
-
+
+ def test_oid(self):
+ """test that a primary key column compiled as the 'oid' column gets proper length truncation"""
+ from sqlalchemy.databases import postgres
+ dialect = postgres.PGDialect()
+ dialect.max_identifier_length = lambda: 30
+ tt = table1.select(use_labels=True).alias('foo')
+ x = select([tt], use_labels=True, order_by=tt.oid_column).compile(dialect=dialect)
+ #print x
+ # assert it doesnt end with "ORDER BY foo.some_large_named_table_this_is_the_primarykey_column"
+ assert str(x).endswith("""ORDER BY foo.some_large_named_table_t_1""")
+
if __name__ == '__main__':
testbase.main()