From 9af6c97504ef2b06dd5292d03ea94d3eb5d8c4d6 Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Thu, 1 Jun 2017 11:51:02 +0200 Subject: [PATCH] Refs #26682 -- Added AutoField introspection on Oracle. --- django/db/backends/oracle/features.py | 1 + django/db/backends/oracle/introspection.py | 23 +++++++++++++++------- docs/releases/2.0.txt | 3 +++ 3 files changed, 20 insertions(+), 7 deletions(-) diff --git a/django/db/backends/oracle/features.py b/django/db/backends/oracle/features.py index af95136fa7..fe6e30dc46 100644 --- a/django/db/backends/oracle/features.py +++ b/django/db/backends/oracle/features.py @@ -11,6 +11,7 @@ class DatabaseFeatures(BaseDatabaseFeatures): has_select_for_update_skip_locked = True can_return_id_from_insert = True allow_sliced_subqueries = False + can_introspect_autofield = True supports_subqueries_in_group_by = False supports_transactions = True supports_timezones = False diff --git a/django/db/backends/oracle/introspection.py b/django/db/backends/oracle/introspection.py index 50d9d8092c..f15b49eb19 100644 --- a/django/db/backends/oracle/introspection.py +++ b/django/db/backends/oracle/introspection.py @@ -1,12 +1,15 @@ import warnings +from collections import namedtuple import cx_Oracle from django.db.backends.base.introspection import ( - BaseDatabaseIntrospection, FieldInfo, TableInfo, + BaseDatabaseIntrospection, FieldInfo as BaseFieldInfo, TableInfo, ) from django.utils.deprecation import RemovedInDjango21Warning +FieldInfo = namedtuple('FieldInfo', BaseFieldInfo._fields + ('is_autofield',)) + class DatabaseIntrospection(BaseDatabaseIntrospection): # Maps type objects to Django Field types. @@ -32,9 +35,11 @@ class DatabaseIntrospection(BaseDatabaseIntrospection): precision, scale = description[4:6] if scale == 0: if precision > 11: - return 'BigIntegerField' + return 'BigAutoField' if description.is_autofield else 'BigIntegerField' elif precision == 1: return 'BooleanField' + elif description.is_autofield: + return 'AutoField' else: return 'IntegerField' elif scale == -127: @@ -61,12 +66,16 @@ class DatabaseIntrospection(BaseDatabaseIntrospection): CASE WHEN char_used IS NULL THEN data_length ELSE char_length - END as internal_size + END as internal_size, + CASE + WHEN identity_column = 'YES' THEN 1 + ELSE 0 + END as is_autofield FROM user_tab_cols WHERE table_name = UPPER(%s)""", [table_name]) field_map = { - column: (internal_size, default if default != 'NULL' else None) - for column, default, internal_size in cursor.fetchall() + column: (internal_size, default if default != 'NULL' else None, is_autofield) + for column, default, internal_size, is_autofield in cursor.fetchall() } self.cache_bust_counter += 1 cursor.execute("SELECT * FROM {} WHERE ROWNUM < 2 AND {} > 0".format( @@ -75,14 +84,14 @@ class DatabaseIntrospection(BaseDatabaseIntrospection): description = [] for desc in cursor.description: name = desc[0] - internal_size, default = field_map[name] + internal_size, default, is_autofield = field_map[name] name = name % {} # cx_Oracle, for some reason, doubles percent signs. description.append(FieldInfo(*( (name.lower(),) + desc[1:3] + (internal_size, desc[4] or 0, desc[5] or 0) + desc[6:] + - (default,) + (default, is_autofield) ))) return description diff --git a/docs/releases/2.0.txt b/docs/releases/2.0.txt index 6666f6d1d5..1070889da0 100644 --- a/docs/releases/2.0.txt +++ b/docs/releases/2.0.txt @@ -195,6 +195,9 @@ Management Commands * The new :option:`diffsettings --output` option allows formatting the output in a unified diff format. +* On Oracle, :djadmin:`inspectdb` can now introspect ``AutoField`` if the + column is created as an identity column. + Migrations ~~~~~~~~~~