Commits

Anonymous committed 9076389

[1.0.X] Fixed #11049: introspection on Oracle now identifies IntegerFields correctly.

Comments (0)

Files changed (5)

django/contrib/gis/management/commands/inspectdb.py

                         if srid != 4326: extra_params['srid'] = srid
                     else:
                         try:
-                            field_type = connection.introspection.data_types_reverse[row[1]]
+                            field_type = connection.introspection.get_field_type(row[1], row)
                         except KeyError:
                             field_type = 'TextField'
                             comment_notes.append('This field type is a guess.')

django/core/management/commands/inspectdb.py

                         extra_params['db_column'] = column_name
                 else:
                     try:
-                        field_type = connection.introspection.data_types_reverse[row[1]]
+                        field_type = connection.introspection.get_field_type(row[1], row)
                     except KeyError:
                         field_type = 'TextField'
                         comment_notes.append('This field type is a guess.')

django/db/backends/__init__.py

     def __init__(self, connection):
         self.connection = connection
 
+    def get_field_type(self, data_type, description):
+        """Hook for a database backend to use the cursor description to
+        match a Django field type to a database column.
+
+        For Oracle, the column data_type on its own is insufficient to
+        distinguish between a FloatField and IntegerField, for example."""
+        return self.data_types_reverse[data_type]
+
     def table_name_converter(self, name):
         """Apply a conversion to the name for the purposes of comparison.
 
     def validate_field(self, errors, opts, f):
         "By default, there is no backend-specific validation"
         pass
-

django/db/backends/oracle/introspection.py

     except AttributeError:
         pass
 
+    def get_field_type(self, data_type, description):
+        # If it's a NUMBER with scale == 0, consider it an IntegerField
+        if data_type == cx_Oracle.NUMBER and description[5] == 0:
+            return 'IntegerField'
+        else:
+            return super(DatabaseIntrospection, self).get_field_type(
+                data_type, description)
+
     def get_table_list(self, cursor):
         "Returns a list of table names in the current database."
         cursor.execute("SELECT TABLE_NAME FROM USER_TABLES")

tests/regressiontests/introspection/tests.py

     def test_get_table_description_types(self):
         cursor = connection.cursor()
         desc = connection.introspection.get_table_description(cursor, Reporter._meta.db_table)
-        self.assertEqual([datatype(r[1]) for r in desc],
+        self.assertEqual([datatype(r[1], r) for r in desc],
                           ['IntegerField', 'CharField', 'CharField', 'CharField'])
 
     # Regression test for #9991 - 'real' types in postgres
             cursor.execute("CREATE TABLE django_ixn_real_test_table (number REAL);")
             desc = connection.introspection.get_table_description(cursor, 'django_ixn_real_test_table')
             cursor.execute('DROP TABLE django_ixn_real_test_table;')
-            self.assertEqual(datatype(desc[0][1]), 'FloatField')
+            self.assertEqual(datatype(desc[0][1], desc[0]), 'FloatField')
 
     def test_get_relations(self):
         cursor = connection.cursor()
         indexes = connection.introspection.get_indexes(cursor, Article._meta.db_table)
         self.assertEqual(indexes['reporter_id'], {'unique': False, 'primary_key': False})
 
-def datatype(dbtype):
+
+def datatype(dbtype, description):
     """Helper to convert a data type into a string."""
-    dt = connection.introspection.data_types_reverse[dbtype]
+    dt = connection.introspection.get_field_type(dbtype, description)
     if type(dt) is tuple:
         return dt[0]
     else: