reflect oracle no-column (functional) index when table has no primray key

Issue #4042 resolved
Mike Bayer repo owner created an issue
from sqlalchemy import *

m = MetaData()

t = Table(
   't', m,
   Column('q', Integer)
)

Index('t_idx_1', func.lower(t.c.q))
Index('t_idx_2', t.c.q)

e = create_engine("oracle://scott:tiger@xe", echo='debug')
m.drop_all(e)
m.create_all(e)

insp = inspect(e)

print insp.get_indexes('t')

fix:

diff --git a/lib/sqlalchemy/dialects/oracle/base.py b/lib/sqlalchemy/dialects/oracle/base.py
index d9fa80d..83a76e5 100644
--- a/lib/sqlalchemy/dialects/oracle/base.py
+++ b/lib/sqlalchemy/dialects/oracle/base.py
@@ -1476,7 +1476,7 @@ class OracleDialect(default.DefaultDialect):

         def remove_if_primary_key(index):
             # don't include the primary key index
-            if index is not None and \
+            if pk_names and index is not None and \
                upper_name_set(index['column_names']) == pk_names:
                 indexes.pop()

Comments (2)

  1. Mike Bayer reporter

    we are also getting the Index back in reflectable(), without a table because no columns, so that needs a fix also

  2. Mike Bayer reporter

    Ensure Oracle index w/ col DESC etc. is reflected

    Fixed bug where an index reflected under Oracle with an expression like "column DESC" would not be returned, if the table also had no primary key, as a result of logic that attempts to filter out the index implicitly added by Oracle onto the primary key columns.

    Reworked the "filter out the primary key index" logic in oracle get_indexes() to be clearer.

    This changeset also adds an internal check to ColumnCollection to accomodate for the case of a column being added twice, as well as adding a private _table argument to Index such that reflection can specify the Table explicitly. The _table argument can become part of public API in a later revision or release if needed.

    Change-Id: I745711e03b3e450b7f31185fc70e10d3823063fa Fixes: #4042

    → <<cset 1a990ee33239>>

  3. Log in to comment