From e263cc0dc53577d525aad0546fc1bd5939dc2e0b Mon Sep 17 00:00:00 2001 From: Matt Boersma Date: Fri, 21 Aug 2009 21:42:39 +0000 Subject: [PATCH] Fixed #11049: introspection on Oracle now identifies IntegerFields correctly. git-svn-id: http://code.djangoproject.com/svn/django/trunk@11475 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/gis/management/commands/inspectdb.py | 2 +- django/core/management/commands/inspectdb.py | 2 +- django/db/backends/__init__.py | 9 ++++++++- django/db/backends/oracle/introspection.py | 8 ++++++++ tests/regressiontests/introspection/tests.py | 9 +++++---- 5 files changed, 23 insertions(+), 7 deletions(-) diff --git a/django/contrib/gis/management/commands/inspectdb.py b/django/contrib/gis/management/commands/inspectdb.py index d4fe210953..365bb24063 100644 --- a/django/contrib/gis/management/commands/inspectdb.py +++ b/django/contrib/gis/management/commands/inspectdb.py @@ -131,7 +131,7 @@ class Command(InspectCommand): 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.') diff --git a/django/core/management/commands/inspectdb.py b/django/core/management/commands/inspectdb.py index f30a00b7b2..fbe539274e 100644 --- a/django/core/management/commands/inspectdb.py +++ b/django/core/management/commands/inspectdb.py @@ -73,7 +73,7 @@ class Command(NoArgsCommand): 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.') diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py index 579bf80aaf..9a1fe30925 100644 --- a/django/db/backends/__init__.py +++ b/django/db/backends/__init__.py @@ -470,6 +470,14 @@ class BaseDatabaseIntrospection(object): 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. @@ -560,4 +568,3 @@ class BaseDatabaseValidation(object): def validate_field(self, errors, opts, f): "By default, there is no backend-specific validation" pass - diff --git a/django/db/backends/oracle/introspection.py b/django/db/backends/oracle/introspection.py index 543e84a8f3..0b4f61a360 100644 --- a/django/db/backends/oracle/introspection.py +++ b/django/db/backends/oracle/introspection.py @@ -26,6 +26,14 @@ class DatabaseIntrospection(BaseDatabaseIntrospection): 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") diff --git a/tests/regressiontests/introspection/tests.py b/tests/regressiontests/introspection/tests.py index 7072dc8e9e..1454e1e3e5 100644 --- a/tests/regressiontests/introspection/tests.py +++ b/tests/regressiontests/introspection/tests.py @@ -76,7 +76,7 @@ class IntrospectionTests(TestCase): 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 @@ -86,7 +86,7 @@ class IntrospectionTests(TestCase): 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() @@ -104,9 +104,10 @@ class IntrospectionTests(TestCase): 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: