Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #11049: introspection on Oracle now identifies IntegerFields co…

…rrectly.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@11475 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit e263cc0dc53577d525aad0546fc1bd5939dc2e0b 1 parent e0ce9d7
Matt Boersma authored August 21, 2009
2  django/contrib/gis/management/commands/inspectdb.py
@@ -131,7 +131,7 @@ def handle_inspection(self):
131 131
                         if srid != 4326: extra_params['srid'] = srid
132 132
                     else:
133 133
                         try:
134  
-                            field_type = connection.introspection.data_types_reverse[row[1]]
  134
+                            field_type = connection.introspection.get_field_type(row[1], row)
135 135
                         except KeyError:
136 136
                             field_type = 'TextField'
137 137
                             comment_notes.append('This field type is a guess.')
2  django/core/management/commands/inspectdb.py
@@ -73,7 +73,7 @@ def handle_inspection(self):
73 73
                         extra_params['db_column'] = column_name
74 74
                 else:
75 75
                     try:
76  
-                        field_type = connection.introspection.data_types_reverse[row[1]]
  76
+                        field_type = connection.introspection.get_field_type(row[1], row)
77 77
                     except KeyError:
78 78
                         field_type = 'TextField'
79 79
                         comment_notes.append('This field type is a guess.')
9  django/db/backends/__init__.py
@@ -470,6 +470,14 @@ class BaseDatabaseIntrospection(object):
470 470
     def __init__(self, connection):
471 471
         self.connection = connection
472 472
 
  473
+    def get_field_type(self, data_type, description):
  474
+        """Hook for a database backend to use the cursor description to
  475
+        match a Django field type to a database column.
  476
+
  477
+        For Oracle, the column data_type on its own is insufficient to
  478
+        distinguish between a FloatField and IntegerField, for example."""
  479
+        return self.data_types_reverse[data_type]
  480
+
473 481
     def table_name_converter(self, name):
474 482
         """Apply a conversion to the name for the purposes of comparison.
475 483
 
@@ -560,4 +568,3 @@ class BaseDatabaseValidation(object):
560 568
     def validate_field(self, errors, opts, f):
561 569
         "By default, there is no backend-specific validation"
562 570
         pass
563  
-
8  django/db/backends/oracle/introspection.py
@@ -26,6 +26,14 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
26 26
     except AttributeError:
27 27
         pass
28 28
 
  29
+    def get_field_type(self, data_type, description):
  30
+        # If it's a NUMBER with scale == 0, consider it an IntegerField
  31
+        if data_type == cx_Oracle.NUMBER and description[5] == 0:
  32
+            return 'IntegerField'
  33
+        else:
  34
+            return super(DatabaseIntrospection, self).get_field_type(
  35
+                data_type, description)
  36
+
29 37
     def get_table_list(self, cursor):
30 38
         "Returns a list of table names in the current database."
31 39
         cursor.execute("SELECT TABLE_NAME FROM USER_TABLES")
9  tests/regressiontests/introspection/tests.py
@@ -76,7 +76,7 @@ def test_get_table_description_names(self):
76 76
     def test_get_table_description_types(self):
77 77
         cursor = connection.cursor()
78 78
         desc = connection.introspection.get_table_description(cursor, Reporter._meta.db_table)
79  
-        self.assertEqual([datatype(r[1]) for r in desc],
  79
+        self.assertEqual([datatype(r[1], r) for r in desc],
80 80
                           ['IntegerField', 'CharField', 'CharField', 'CharField'])
81 81
 
82 82
     # Regression test for #9991 - 'real' types in postgres
@@ -86,7 +86,7 @@ def test_postgresql_real_type(self):
86 86
             cursor.execute("CREATE TABLE django_ixn_real_test_table (number REAL);")
87 87
             desc = connection.introspection.get_table_description(cursor, 'django_ixn_real_test_table')
88 88
             cursor.execute('DROP TABLE django_ixn_real_test_table;')
89  
-            self.assertEqual(datatype(desc[0][1]), 'FloatField')
  89
+            self.assertEqual(datatype(desc[0][1], desc[0]), 'FloatField')
90 90
 
91 91
     def test_get_relations(self):
92 92
         cursor = connection.cursor()
@@ -104,9 +104,10 @@ def test_get_indexes(self):
104 104
         indexes = connection.introspection.get_indexes(cursor, Article._meta.db_table)
105 105
         self.assertEqual(indexes['reporter_id'], {'unique': False, 'primary_key': False})
106 106
 
107  
-def datatype(dbtype):
  107
+
  108
+def datatype(dbtype, description):
108 109
     """Helper to convert a data type into a string."""
109  
-    dt = connection.introspection.data_types_reverse[dbtype]
  110
+    dt = connection.introspection.get_field_type(dbtype, description)
110 111
     if type(dt) is tuple:
111 112
         return dt[0]
112 113
     else:

0 notes on commit e263cc0

Please sign in to comment.
Something went wrong with that request. Please try again.