Skip to content

Commit 372736b

Browse files
committed
[1.0.X] Fixed django#11049: introspection on Oracle now identifies IntegerFields correctly.
git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@11476 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 0f70fd9 commit 372736b

File tree

5 files changed

+23
-7
lines changed

5 files changed

+23
-7
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ def handle_inspection(self):
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.')

django/core/management/commands/inspectdb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def handle_inspection(self):
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/db/backends/__init__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,14 @@ class BaseDatabaseIntrospection(object):
384384
def __init__(self, connection):
385385
self.connection = connection
386386

387+
def get_field_type(self, data_type, description):
388+
"""Hook for a database backend to use the cursor description to
389+
match a Django field type to a database column.
390+
391+
For Oracle, the column data_type on its own is insufficient to
392+
distinguish between a FloatField and IntegerField, for example."""
393+
return self.data_types_reverse[data_type]
394+
387395
def table_name_converter(self, name):
388396
"""Apply a conversion to the name for the purposes of comparison.
389397
@@ -466,4 +474,3 @@ class BaseDatabaseValidation(object):
466474
def validate_field(self, errors, opts, f):
467475
"By default, there is no backend-specific validation"
468476
pass
469-

django/db/backends/oracle/introspection.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,14 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
2626
except AttributeError:
2727
pass
2828

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+
2937
def get_table_list(self, cursor):
3038
"Returns a list of table names in the current database."
3139
cursor.execute("SELECT TABLE_NAME FROM USER_TABLES")

tests/regressiontests/introspection/tests.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def test_get_table_description_names(self):
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
@@ -86,7 +86,7 @@ def test_postgresql_real_type(self):
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()
@@ -104,9 +104,10 @@ def test_get_indexes(self):
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+
108+
def 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:

0 commit comments

Comments
 (0)