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
This commit is contained in:
Matt Boersma 2009-08-21 21:42:39 +00:00
parent e0ce9d76e1
commit e263cc0dc5
5 changed files with 23 additions and 7 deletions

View File

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

View File

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

View File

@ -470,6 +470,14 @@ class BaseDatabaseIntrospection(object):
def __init__(self, connection): def __init__(self, connection):
self.connection = 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): def table_name_converter(self, name):
"""Apply a conversion to the name for the purposes of comparison. """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): def validate_field(self, errors, opts, f):
"By default, there is no backend-specific validation" "By default, there is no backend-specific validation"
pass pass

View File

@ -26,6 +26,14 @@ class DatabaseIntrospection(BaseDatabaseIntrospection):
except AttributeError: except AttributeError:
pass 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): def get_table_list(self, cursor):
"Returns a list of table names in the current database." "Returns a list of table names in the current database."
cursor.execute("SELECT TABLE_NAME FROM USER_TABLES") cursor.execute("SELECT TABLE_NAME FROM USER_TABLES")

View File

@ -76,7 +76,7 @@ class IntrospectionTests(TestCase):
def test_get_table_description_types(self): def test_get_table_description_types(self):
cursor = connection.cursor() cursor = connection.cursor()
desc = connection.introspection.get_table_description(cursor, Reporter._meta.db_table) 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']) ['IntegerField', 'CharField', 'CharField', 'CharField'])
# Regression test for #9991 - 'real' types in postgres # 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);") cursor.execute("CREATE TABLE django_ixn_real_test_table (number REAL);")
desc = connection.introspection.get_table_description(cursor, 'django_ixn_real_test_table') desc = connection.introspection.get_table_description(cursor, 'django_ixn_real_test_table')
cursor.execute('DROP TABLE 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): def test_get_relations(self):
cursor = connection.cursor() cursor = connection.cursor()
@ -104,9 +104,10 @@ class IntrospectionTests(TestCase):
indexes = connection.introspection.get_indexes(cursor, Article._meta.db_table) indexes = connection.introspection.get_indexes(cursor, Article._meta.db_table)
self.assertEqual(indexes['reporter_id'], {'unique': False, 'primary_key': False}) 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.""" """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: if type(dt) is tuple:
return dt[0] return dt[0]
else: else: