From: Mike Bayer Date: Tue, 6 Jun 2006 16:38:30 +0000 (+0000) Subject: fixed bug where tables with schema name werent getting indexed in metadata correctly X-Git-Tag: rel_0_2_3~23 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d37d8681e4f47162c51bf0348bcf908fac05747e;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git fixed bug where tables with schema name werent getting indexed in metadata correctly --- diff --git a/CHANGES b/CHANGES index 9d200b8a05..6e4dfbd07c 100644 --- a/CHANGES +++ b/CHANGES @@ -3,6 +3,8 @@ "set" not available/ordering is needed. - "foreignkey" argument to relation() can also be a list. fixed auto-foreignkey detection [ticket:151] +- fixed bug where tables with schema names werent getting indexed in +the MetaData object properly 0.2.2 - big improvements to polymorphic inheritance behavior, enabling it diff --git a/lib/sqlalchemy/databases/information_schema.py b/lib/sqlalchemy/databases/information_schema.py index 3dc4a337b7..08236f7991 100644 --- a/lib/sqlalchemy/databases/information_schema.py +++ b/lib/sqlalchemy/databases/information_schema.py @@ -56,15 +56,15 @@ pg_key_constraints = schema.Table("key_column_usage", ischema, Column("constraint_name", String), schema="information_schema") -mysql_key_constraints = schema.Table("key_column_usage", ischema, - Column("table_schema", String), - Column("table_name", String), - Column("column_name", String), - Column("constraint_name", String), - Column("referenced_table_schema", String), - Column("referenced_table_name", String), - Column("referenced_column_name", String), - schema="information_schema") +#mysql_key_constraints = schema.Table("key_column_usage", ischema, +# Column("table_schema", String), +# Column("table_name", String), +# Column("column_name", String), +# Column("constraint_name", String), +# Column("referenced_table_schema", String), +# Column("referenced_table_name", String), +# Column("referenced_column_name", String), +# schema="information_schema") key_constraints = pg_key_constraints diff --git a/lib/sqlalchemy/schema.py b/lib/sqlalchemy/schema.py index 31b3259902..368cb2460f 100644 --- a/lib/sqlalchemy/schema.py +++ b/lib/sqlalchemy/schema.py @@ -156,10 +156,7 @@ class Table(SchemaItem, sql.TableClause): , ',\n') def __str__(self): - if self.schema is None: - return self.name - else: - return self.schema + "." + self.name + return _get_table_key(self.name, self.schema) def reload_values(self, *args): """clears out the columns and other properties of this Table, and reloads them from the @@ -184,7 +181,7 @@ class Table(SchemaItem, sql.TableClause): self.indexes[index.name] = index def _set_parent(self, metadata): - metadata.tables[self.name] = self + metadata.tables[_get_table_key(self.name, self.schema)] = self self._metadata = metadata def accept_schema_visitor(self, visitor): """traverses the given visitor across the Column objects inside this Table, diff --git a/test/engine/reflection.py b/test/engine/reflection.py index bb0fd95898..49f69fdb56 100644 --- a/test/engine/reflection.py +++ b/test/engine/reflection.py @@ -1,13 +1,12 @@ import sqlalchemy.ansisql as ansisql -import sqlalchemy.databases.postgres as postgres from sqlalchemy import * -from sqlalchemy.exceptions import * +from sqlalchemy.exceptions import NoSuchTableError from testbase import PersistTest import testbase -import unittest, re +import unittest, re, StringIO class ReflectionTest(PersistTest): def testbasic(self): @@ -221,8 +220,32 @@ class CreateDropTest(PersistTest): metadata.drop_all(engine=testbase.db) self.assertEqual( testbase.db.has_table('items'), False ) - - +class SchemaTest(PersistTest): + # this test should really be in the sql tests somewhere, not engine + def testiteration(self): + metadata = MetaData() + table1 = Table('table1', metadata, + Column('col1', Integer, primary_key=True), + schema='someschema') + table2 = Table('table2', metadata, + Column('col1', Integer, primary_key=True), + Column('col2', Integer, ForeignKey('someschema.table1.col1')), + schema='someschema') + # insure this doesnt crash + print [t for t in metadata.table_iterator()] + buf = StringIO.StringIO() + def foo(s, p): + buf.write(s) + gen = testbase.db.dialect.schemagenerator(testbase.db.engine, foo) + table1.accept_schema_visitor(gen) + table2.accept_schema_visitor(gen) + buf = buf.getvalue() + assert buf.index("CREATE TABLE someschema.table1") > -1 + assert buf.index("CREATE TABLE someschema.table2") > -1 + + + + if __name__ == "__main__": testbase.main()