From: Mike Bayer Date: Mon, 7 May 2007 20:29:26 +0000 (+0000) Subject: - fix to long name generation when using oid_column as an order by X-Git-Tag: rel_0_3_8~50 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e23c3a89742712c427c7e642186fc32263652530;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git - fix to long name generation when using oid_column as an order by (oids used heavily in mapper queries) --- diff --git a/CHANGES b/CHANGES index 64f59d05c0..193025ab29 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 cd6e0858fa..ab043f3ec9 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 7d458da06f..ee9fa6bc50 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()