]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- Fixed bug where Oracle table reflection using synonyms would fail
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 25 Oct 2013 23:11:53 +0000 (19:11 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 25 Oct 2013 23:11:53 +0000 (19:11 -0400)
if the synonym and the table were in different remote schemas.
Patch to fix courtesy Kyle Derr. [ticket:2853]

doc/build/changelog/changelog_08.rst
lib/sqlalchemy/dialects/oracle/base.py
test/dialect/test_oracle.py

index ab36c76f86f76f0255ec4833c18e38a936fdec1b..c48ac9aa77e65ef7e7a2e7fc0c0acd1c31562000 100644 (file)
 .. changelog::
     :version: 0.8.3
 
+    .. change::
+        :tags: bug, oracle
+        :tickets: 2853
+        :versions: 0.9.0
+
+        Fixed bug where Oracle table reflection using synonyms would fail
+        if the synonym and the table were in different remote schemas.
+        Patch to fix courtesy Kyle Derr.
+
     .. change::
         :tags: bug, sql
         :tickets: 2849
index 54c254c0f4d69edb91e5c2532baa78306284b17d..1f5e05cba9575586c19348355acb05604e56a54c 100644 (file)
@@ -168,9 +168,10 @@ Synonym/DBLINK Reflection
 -------------------------
 
 When using reflection with Table objects, the dialect can optionally search for tables
-indicated by synonyms that reference DBLINK-ed tables by passing the flag
-oracle_resolve_synonyms=True as a keyword argument to the Table construct.  If DBLINK
-is not in use this flag should be left off.
+indicated by synonyms, either in local or remote schemas or accessed over DBLINK,
+by passing the flag oracle_resolve_synonyms=True as a
+keyword argument to the Table construct.   If synonyms are not in use
+this flag should be left off.
 
 """
 
@@ -834,14 +835,15 @@ class OracleDialect(default.DefaultDialect):
         returns the actual name, owner, dblink name, and synonym name if found.
         """
 
-        q = "SELECT owner, table_owner, table_name, db_link, synonym_name FROM all_synonyms WHERE "
+        q = "SELECT owner, table_owner, table_name, db_link, "\
+                    "synonym_name FROM all_synonyms WHERE "
         clauses = []
         params = {}
         if desired_synonym:
             clauses.append("synonym_name = :synonym_name")
             params['synonym_name'] = desired_synonym
         if desired_owner:
-            clauses.append("table_owner = :desired_owner")
+            clauses.append("owner = :desired_owner")
             params['desired_owner'] = desired_owner
         if desired_table:
             clauses.append("table_name = :tname")
index 1fde1c1987e0450b4ae12a4628b55ced9c9a18c8..d843a2d9ebabbe64ee80ffed6cb3bcf4b51280c8 100644 (file)
@@ -675,9 +675,18 @@ create table test_schema.child(
     parent_id integer references test_schema.parent(id)
 );
 
+create table local_table(
+    id integer primary key,
+    data varchar2(50)
+);
+
 create synonym test_schema.ptable for test_schema.parent;
 create synonym test_schema.ctable for test_schema.child;
 
+create synonym test_schema_ptable for test_schema.parent;
+
+create synonym test_schema.local_table for local_table;
+
 -- can't make a ref from local schema to the
 -- remote schema's table without this,
 -- *and* cant give yourself a grant !
@@ -693,8 +702,12 @@ grant references on test_schema.child to public;
         for stmt in """
 drop table test_schema.child;
 drop table test_schema.parent;
+drop table local_table;
 drop synonym test_schema.ctable;
 drop synonym test_schema.ptable;
+drop synonym test_schema_ptable;
+drop synonym test_schema.local_table;
+
 """.split(";"):
             if stmt.strip():
                 testing.db.execute(stmt)
@@ -719,6 +732,22 @@ drop synonym test_schema.ptable;
         finally:
             meta.drop_all()
 
+    def test_reflect_alt_table_owner_local_synonym(self):
+        meta = MetaData(testing.db)
+        parent = Table('test_schema_ptable', meta, autoload=True, oracle_resolve_synonyms=True)
+        self.assert_compile(parent.select(),
+                "SELECT test_schema_ptable.id, "
+                "test_schema_ptable.data FROM test_schema_ptable")
+        select([parent]).execute().fetchall()
+
+    def test_reflect_alt_synonym_owner_local_table(self):
+        meta = MetaData(testing.db)
+        parent = Table('local_table', meta, autoload=True, oracle_resolve_synonyms=True, schema="test_schema")
+        self.assert_compile(parent.select(),
+                "SELECT test_schema.local_table.id, "
+                "test_schema.local_table.data FROM test_schema.local_table")
+        select([parent]).execute().fetchall()
+
     def test_create_same_names_implicit_schema(self):
         meta = MetaData(testing.db)
         parent = Table('parent', meta,