From 5b92c3d097b487001c4152715a22c4de87fad871 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sun, 14 Oct 2007 19:15:09 +0000 Subject: [PATCH] - PG reflection, upon seeing the default schema name being used explicitly as the "schema" argument in a Table, will assume that this is the the user's desired convention, and will explicitly set the "schema" argument in foreign-key-related reflected tables, thus making them match only with Table constructors that also use the explicit "schema" argument (even though its the default schema). In other words, SA assumes the user is being consistent in this usage. --- CHANGES | 8 ++++++++ lib/sqlalchemy/databases/postgres.py | 5 +++++ test/engine/reflection.py | 8 +++----- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/CHANGES b/CHANGES index 7f1543e868..cebf107c7d 100644 --- a/CHANGES +++ b/CHANGES @@ -38,6 +38,14 @@ CHANGES - Added support for returning values from inserts and udpates for PostgreSQL 8.2+. [ticket:797] +- PG reflection, upon seeing the default schema name being used explicitly + as the "schema" argument in a Table, will assume that this is the the + user's desired convention, and will explicitly set the "schema" argument + in foreign-key-related reflected tables, thus making them match only + with Table constructors that also use the explicit "schema" argument + (even though its the default schema). + In other words, SA assumes the user is being consistent in this usage. + - fixed sqlite reflection of BOOL/BOOLEAN [ticket:808] - null foreign key on a m2o doesn't trigger a lazyload [ticket:803] diff --git a/lib/sqlalchemy/databases/postgres.py b/lib/sqlalchemy/databases/postgres.py index d894b1f2cf..ddf6a6b9ce 100644 --- a/lib/sqlalchemy/databases/postgres.py +++ b/lib/sqlalchemy/databases/postgres.py @@ -545,6 +545,11 @@ class PGDialect(default.DefaultDialect): constrained_columns = [preparer._unquote_identifier(x) for x in re.split(r'\s*,\s*', constrained_columns)] if referred_schema: referred_schema = preparer._unquote_identifier(referred_schema) + elif table.schema is not None and table.schema == self.get_default_schema_name(connection): + # no schema (i.e. its the default schema), and the table we're + # reflecting has the default schema explicit, then use that. + # i.e. try to use the user's conventions + referred_schema = table.schema referred_table = preparer._unquote_identifier(referred_table) referred_columns = [preparer._unquote_identifier(x) for x in re.split(r'\s*,\s', referred_columns)] diff --git a/test/engine/reflection.py b/test/engine/reflection.py index 82a04874af..4f1d18d5d2 100644 --- a/test/engine/reflection.py +++ b/test/engine/reflection.py @@ -696,7 +696,7 @@ class UnicodeTest(PersistTest): class SchemaTest(PersistTest): # this test should really be in the sql tests somewhere, not engine @testing.unsupported('sqlite', 'firebird') - def testiteration(self): + def test_iteration(self): metadata = MetaData() table1 = Table('table1', metadata, Column('col1', Integer, primary_key=True), @@ -719,10 +719,8 @@ class SchemaTest(PersistTest): assert buf.index("CREATE TABLE someschema.table1") > -1 assert buf.index("CREATE TABLE someschema.table2") > -1 - # TODO: figure out why postgres screws up on this test - #@testing.supported('mysql','postgres') - @testing.supported('mysql') - def testcreate(self): + @testing.supported('mysql','postgres') + def test_explicit_default_schema(self): engine = testbase.db schema = engine.dialect.get_default_schema_name(engine) #engine.echo = True -- 2.47.3