Ticket #11049: 11049_fix.diff

File 11049_fix.diff, 4.9 KB (added by Matt Boersma, 15 years ago)

A fix to allow Oracle to properly introspect IntegerFields, created against trunk [10748]

  • django/db/backends/oracle/introspection.py

     
    2121    except AttributeError:
    2222        pass
    2323
     24    def get_field_type(self, data_type, description):
     25        # If it's a NUMBER with scale == 0, consider it an IntegerField
     26        if data_type == cx_Oracle.NUMBER and description[5] == 0:
     27            return 'IntegerField'
     28        else:
     29            return super(DatabaseIntrospection, self).get_field_type(
     30                data_type, description)
     31
    2432    def get_table_list(self, cursor):
    2533        "Returns a list of table names in the current database."
    2634        cursor.execute("SELECT TABLE_NAME FROM USER_TABLES")
  • django/db/backends/__init__.py

     
    470470    def __init__(self, connection):
    471471        self.connection = connection
    472472
     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
    473481    def table_name_converter(self, name):
    474482        """Apply a conversion to the name for the purposes of comparison.
    475483
     
    560568    def validate_field(self, errors, opts, f):
    561569        "By default, there is no backend-specific validation"
    562570        pass
    563 
  • django/core/management/commands/inspectdb.py

     
    7373                        extra_params['db_column'] = column_name
    7474                else:
    7575                    try:
    76                         field_type = connection.introspection.data_types_reverse[row[1]]
     76                        field_type = connection.introspection.get_field_type(row[1], row)
    7777                    except KeyError:
    7878                        field_type = 'TextField'
    7979                        comment_notes.append('This field type is a guess.')
  • django/contrib/gis/management/commands/inspectdb.py

     
    131131                        if srid != 4326: extra_params['srid'] = srid
    132132                    else:
    133133                        try:
    134                             field_type = connection.introspection.data_types_reverse[row[1]]
     134                            field_type = connection.introspection.get_field_type(row[1], row)
    135135                        except KeyError:
    136136                            field_type = 'TextField'
    137137                            comment_notes.append('This field type is a guess.')
  • tests/regressiontests/introspection/tests.py

     
    7676    def test_get_table_description_types(self):
    7777        cursor = connection.cursor()
    7878        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],
    8080                          ['IntegerField', 'CharField', 'CharField', 'CharField'])
    8181
    8282    # Regression test for #9991 - 'real' types in postgres
     
    8686            cursor.execute("CREATE TABLE django_ixn_real_test_table (number REAL);")
    8787            desc = connection.introspection.get_table_description(cursor, 'django_ixn_real_test_table')
    8888            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')
    9090
    9191    def test_get_relations(self):
    9292        cursor = connection.cursor()
     
    104104        indexes = connection.introspection.get_indexes(cursor, Article._meta.db_table)
    105105        self.assertEqual(indexes['reporter_id'], {'unique': False, 'primary_key': False})
    106106
    107 def datatype(dbtype):
     107
     108def datatype(dbtype, description):
    108109    """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)
    110111    if type(dt) is tuple:
    111112        return dt[0]
    112113    else:
Back to Top