Oracle reflection does not set the name of Primary key constraints

Issue #1868 resolved
Former user created an issue

My patch:

--- lib/sqlalchemy/dialects/oracle/base.py      2010-08-03 07:18:07.000000000 -0400
+++ /home/rarch/site-packages/SQLAlchemy-0.6.1.4-py2.6-linux-x86_64.egg/sqlalchemy/dialects/oracle/base.py      2010-08-03 11:39:54.000000000 -0400
@@ -929,43 +929,58 @@
     @reflection.cache
     def get_primary_keys(self, connection, table_name, schema=None, **kw):
         """

         kw arguments can be:

             oracle_resolve_synonyms

             dblink

         """
+        return self._get_primary_keys(connection, table_name, schema=None, **kw)[0](0)

+    @reflection.cache
+    def _get_primary_keys(self, connection, table_name, schema=None, **kw):
         resolve_synonyms = kw.get('oracle_resolve_synonyms', False)
         dblink = kw.get('dblink', '')
         info_cache = kw.get('info_cache')

         (table_name, schema, dblink, synonym) = \
             self._prepare_reflection_args(connection, table_name, schema,
                                           resolve_synonyms, dblink,
                                           info_cache=info_cache)
         pkeys = [       constraint_name = None
         constraint_data = self._get_constraint_data(connection, table_name,
                                         schema, dblink,
                                         info_cache=kw.get('info_cache'))

         for row in constraint_data:
             #print "ROW:" , row
             (cons_name, cons_type, local_column, remote_table, remote_column, remote_owner) = \
                 row[0:2](]
+) + tuple([for x in row[2:6](self.normalize_name(x))])
             if cons_type == 'P':
+                if constraint_name is None:
+                    constraint_name = self.normalize_name(cons_name)
                 pkeys.append(local_column)
-        return pkeys
+        return pkeys, constraint_name
+
+    @reflection.cache
+    def get_pk_constraint(self, connection, table_name, schema=None, **kw):
+        cols, name = self._get_primary_keys(connection, table_name, schema=schema, **kw)
+
+        return {
+            'constrained_columns':cols,
+            'name':name
+        }

     @reflection.cache
     def get_foreign_keys(self, connection, table_name, schema=None, **kw):
         """

         kw arguments can be:

             oracle_resolve_synonyms

             dblink

Note

You may consider removing @reflection.cache from get_primary_keys() (and get_pk_constraint()) and keep it only on _get_primary_keys()

The attached file's output before patch

type: PrimaryKeyConstraint()
name: None
columns: [u'id_b'](u'id_a',)
(u'SYS_C0048811', u'P', u'ID_A', None, None, None, Decimal('1'), None)
(u'SYS_C0048811', u'P', u'ID_B', None, None, None, Decimal('2'), None)

after patch

type: PrimaryKeyConstraint()
name: sys_c0048811
columns: [u'id_b'](u'id_a',)
(u'SYS_C0048811', u'P', u'ID_A', None, None, None, Decimal('1'), None)
(u'SYS_C0048811', u'P', u'ID_B', None, None, None, Decimal('2'), None)

Thanks,

Kent

Comments (5)

  1. Log in to comment