diff options
| author | Nick Pope <nick.pope@flightdataservices.com> | 2019-07-26 22:05:22 +0100 |
|---|---|---|
| committer | Mariusz Felisiak <felisiak.mariusz@gmail.com> | 2019-08-02 11:39:01 +0200 |
| commit | 194d1dfc186cc8d2b35dabf64f3ed38b757cbd98 (patch) | |
| tree | 51ffabb34edc5b191ce8079c9149b77c88c2749e /django/db | |
| parent | 955b382600e4626265cc20e5773bdbcfd01fc4af (diff) | |
Fixed #30661 -- Added models.SmallAutoField.
Diffstat (limited to 'django/db')
| -rw-r--r-- | django/db/backends/base/features.py | 5 | ||||
| -rw-r--r-- | django/db/backends/base/schema.py | 2 | ||||
| -rw-r--r-- | django/db/backends/mysql/base.py | 1 | ||||
| -rw-r--r-- | django/db/backends/mysql/introspection.py | 2 | ||||
| -rw-r--r-- | django/db/backends/mysql/operations.py | 1 | ||||
| -rw-r--r-- | django/db/backends/oracle/base.py | 1 | ||||
| -rw-r--r-- | django/db/backends/oracle/introspection.py | 2 | ||||
| -rw-r--r-- | django/db/backends/oracle/operations.py | 1 | ||||
| -rw-r--r-- | django/db/backends/oracle/schema.py | 2 | ||||
| -rw-r--r-- | django/db/backends/postgresql/base.py | 1 | ||||
| -rw-r--r-- | django/db/backends/postgresql/introspection.py | 2 | ||||
| -rw-r--r-- | django/db/backends/postgresql/operations.py | 1 | ||||
| -rw-r--r-- | django/db/backends/postgresql/schema.py | 6 | ||||
| -rw-r--r-- | django/db/backends/sqlite3/base.py | 2 | ||||
| -rw-r--r-- | django/db/backends/sqlite3/features.py | 1 | ||||
| -rw-r--r-- | django/db/backends/sqlite3/introspection.py | 6 | ||||
| -rw-r--r-- | django/db/models/fields/__init__.py | 14 |
17 files changed, 38 insertions, 12 deletions
diff --git a/django/db/backends/base/features.py b/django/db/backends/base/features.py index bb4b59e7c8..70878f92a2 100644 --- a/django/db/backends/base/features.py +++ b/django/db/backends/base/features.py @@ -143,9 +143,10 @@ class BaseDatabaseFeatures: # Can the backend introspect a TimeField, instead of a DateTimeField? can_introspect_time_field = True - # Some backends may not be able to differentiate BigAutoField from other - # fields such as AutoField. + # Some backends may not be able to differentiate BigAutoField or + # SmallAutoField from other fields such as AutoField. introspected_big_auto_field_type = 'BigAutoField' + introspected_small_auto_field_type = 'SmallAutoField' # Some backends may not be able to differentiate BooleanField from other # fields such as IntegerField. diff --git a/django/db/backends/base/schema.py b/django/db/backends/base/schema.py index a5a4163c19..3540fd5d0d 100644 --- a/django/db/backends/base/schema.py +++ b/django/db/backends/base/schema.py @@ -182,7 +182,7 @@ class BaseDatabaseSchemaEditor: )) # Autoincrement SQL (for backends with post table definition # variant). - if field.get_internal_type() in ('AutoField', 'BigAutoField'): + if field.get_internal_type() in ('AutoField', 'BigAutoField', 'SmallAutoField'): autoinc_sql = self.connection.ops.autoinc_sql(model._meta.db_table, field.column) if autoinc_sql: self.deferred_sql.extend(autoinc_sql) diff --git a/django/db/backends/mysql/base.py b/django/db/backends/mysql/base.py index 821f24fdf1..9a0ede0ac8 100644 --- a/django/db/backends/mysql/base.py +++ b/django/db/backends/mysql/base.py @@ -123,6 +123,7 @@ class DatabaseWrapper(BaseDatabaseWrapper): 'PositiveIntegerField': 'integer UNSIGNED', 'PositiveSmallIntegerField': 'smallint UNSIGNED', 'SlugField': 'varchar(%(max_length)s)', + 'SmallAutoField': 'smallint AUTO_INCREMENT', 'SmallIntegerField': 'smallint', 'TextField': 'longtext', 'TimeField': 'time(6)', diff --git a/django/db/backends/mysql/introspection.py b/django/db/backends/mysql/introspection.py index db1a387679..05e73c78dc 100644 --- a/django/db/backends/mysql/introspection.py +++ b/django/db/backends/mysql/introspection.py @@ -44,6 +44,8 @@ class DatabaseIntrospection(BaseDatabaseIntrospection): return 'AutoField' elif field_type == 'BigIntegerField': return 'BigAutoField' + elif field_type == 'SmallIntegerField': + return 'SmallAutoField' if description.is_unsigned: if field_type == 'IntegerField': return 'PositiveIntegerField' diff --git a/django/db/backends/mysql/operations.py b/django/db/backends/mysql/operations.py index ef908ad8ae..e940286720 100644 --- a/django/db/backends/mysql/operations.py +++ b/django/db/backends/mysql/operations.py @@ -19,6 +19,7 @@ class DatabaseOperations(BaseDatabaseOperations): cast_data_types = { 'AutoField': 'signed integer', 'BigAutoField': 'signed integer', + 'SmallAutoField': 'signed integer', 'CharField': 'char(%(max_length)s)', 'DecimalField': 'decimal(%(max_digits)s, %(decimal_places)s)', 'TextField': 'char', diff --git a/django/db/backends/oracle/base.py b/django/db/backends/oracle/base.py index 0fbe96ab9b..ef33d9fad7 100644 --- a/django/db/backends/oracle/base.py +++ b/django/db/backends/oracle/base.py @@ -123,6 +123,7 @@ class DatabaseWrapper(BaseDatabaseWrapper): 'PositiveIntegerField': 'NUMBER(11)', 'PositiveSmallIntegerField': 'NUMBER(11)', 'SlugField': 'NVARCHAR2(%(max_length)s)', + 'SmallAutoField': 'NUMBER(5) GENERATED BY DEFAULT ON NULL AS IDENTITY', 'SmallIntegerField': 'NUMBER(11)', 'TextField': 'NCLOB', 'TimeField': 'TIMESTAMP', diff --git a/django/db/backends/oracle/introspection.py b/django/db/backends/oracle/introspection.py index 1fcba05510..2322ae0b5d 100644 --- a/django/db/backends/oracle/introspection.py +++ b/django/db/backends/oracle/introspection.py @@ -35,6 +35,8 @@ class DatabaseIntrospection(BaseDatabaseIntrospection): if scale == 0: if precision > 11: return 'BigAutoField' if description.is_autofield else 'BigIntegerField' + elif 1 < precision < 6 and description.is_autofield: + return 'SmallAutoField' elif precision == 1: return 'BooleanField' elif description.is_autofield: diff --git a/django/db/backends/oracle/operations.py b/django/db/backends/oracle/operations.py index 77c568e84b..016d4c9a59 100644 --- a/django/db/backends/oracle/operations.py +++ b/django/db/backends/oracle/operations.py @@ -56,6 +56,7 @@ END; cast_data_types = { 'AutoField': 'NUMBER(11)', 'BigAutoField': 'NUMBER(19)', + 'SmallAutoField': 'NUMBER(5)', 'TextField': cast_char_field_without_max_length, } diff --git a/django/db/backends/oracle/schema.py b/django/db/backends/oracle/schema.py index 3356788a82..d0f664a6ff 100644 --- a/django/db/backends/oracle/schema.py +++ b/django/db/backends/oracle/schema.py @@ -90,7 +90,7 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): # Make a new field that's like the new one but with a temporary # column name. new_temp_field = copy.deepcopy(new_field) - new_temp_field.null = (new_field.get_internal_type() not in ('AutoField', 'BigAutoField')) + new_temp_field.null = (new_field.get_internal_type() not in ('AutoField', 'BigAutoField', 'SmallAutoField')) new_temp_field.column = self._generate_temp_name(new_field.column) # Add it self.add_field(model, new_temp_field) diff --git a/django/db/backends/postgresql/base.py b/django/db/backends/postgresql/base.py index 7e34a3a177..eea8aeb135 100644 --- a/django/db/backends/postgresql/base.py +++ b/django/db/backends/postgresql/base.py @@ -92,6 +92,7 @@ class DatabaseWrapper(BaseDatabaseWrapper): 'PositiveIntegerField': 'integer', 'PositiveSmallIntegerField': 'smallint', 'SlugField': 'varchar(%(max_length)s)', + 'SmallAutoField': 'smallserial', 'SmallIntegerField': 'smallint', 'TextField': 'text', 'TimeField': 'time', diff --git a/django/db/backends/postgresql/introspection.py b/django/db/backends/postgresql/introspection.py index 1c9c7e63a5..b7bf18691c 100644 --- a/django/db/backends/postgresql/introspection.py +++ b/django/db/backends/postgresql/introspection.py @@ -37,6 +37,8 @@ class DatabaseIntrospection(BaseDatabaseIntrospection): return 'AutoField' elif field_type == 'BigIntegerField': return 'BigAutoField' + elif field_type == 'SmallIntegerField': + return 'SmallAutoField' return field_type def get_table_list(self, cursor): diff --git a/django/db/backends/postgresql/operations.py b/django/db/backends/postgresql/operations.py index 0c497edf71..61bac5e55a 100644 --- a/django/db/backends/postgresql/operations.py +++ b/django/db/backends/postgresql/operations.py @@ -11,6 +11,7 @@ class DatabaseOperations(BaseDatabaseOperations): cast_data_types = { 'AutoField': 'integer', 'BigAutoField': 'bigint', + 'SmallAutoField': 'smallint', } def unification_cast_sql(self, output_field): diff --git a/django/db/backends/postgresql/schema.py b/django/db/backends/postgresql/schema.py index b7f52ccf25..eb5b182680 100644 --- a/django/db/backends/postgresql/schema.py +++ b/django/db/backends/postgresql/schema.py @@ -69,15 +69,15 @@ class DatabaseSchemaEditor(BaseDatabaseSchemaEditor): self.sql_alter_column_type += ' USING %(column)s::%(type)s' # Make ALTER TYPE with SERIAL make sense. table = strip_quotes(model._meta.db_table) - if new_type.lower() in ("serial", "bigserial"): + serial_fields_map = {'bigserial': 'bigint', 'serial': 'integer', 'smallserial': 'smallint'} + if new_type.lower() in serial_fields_map: column = strip_quotes(new_field.column) sequence_name = "%s_%s_seq" % (table, column) - col_type = "integer" if new_type.lower() == "serial" else "bigint" return ( ( self.sql_alter_column_type % { "column": self.quote_name(column), - "type": col_type, + "type": serial_fields_map[new_type.lower()], }, [], ), diff --git a/django/db/backends/sqlite3/base.py b/django/db/backends/sqlite3/base.py index fff65197f9..7a4439ae88 100644 --- a/django/db/backends/sqlite3/base.py +++ b/django/db/backends/sqlite3/base.py @@ -104,6 +104,7 @@ class DatabaseWrapper(BaseDatabaseWrapper): 'PositiveIntegerField': 'integer unsigned', 'PositiveSmallIntegerField': 'smallint unsigned', 'SlugField': 'varchar(%(max_length)s)', + 'SmallAutoField': 'integer', 'SmallIntegerField': 'smallint', 'TextField': 'text', 'TimeField': 'time', @@ -116,6 +117,7 @@ class DatabaseWrapper(BaseDatabaseWrapper): data_types_suffix = { 'AutoField': 'AUTOINCREMENT', 'BigAutoField': 'AUTOINCREMENT', + 'SmallAutoField': 'AUTOINCREMENT', } # SQLite requires LIKE statements to include an ESCAPE clause if the value # being escaped has a percent or underscore in it. diff --git a/django/db/backends/sqlite3/features.py b/django/db/backends/sqlite3/features.py index 3d52a8036f..18b76f8c86 100644 --- a/django/db/backends/sqlite3/features.py +++ b/django/db/backends/sqlite3/features.py @@ -19,6 +19,7 @@ class DatabaseFeatures(BaseDatabaseFeatures): can_introspect_positive_integer_field = True can_introspect_small_integer_field = True introspected_big_auto_field_type = 'AutoField' + introspected_small_auto_field_type = 'AutoField' supports_transactions = True atomic_transactions = False can_rollback_ddl = True diff --git a/django/db/backends/sqlite3/introspection.py b/django/db/backends/sqlite3/introspection.py index faf9328415..5e33d2a7e2 100644 --- a/django/db/backends/sqlite3/introspection.py +++ b/django/db/backends/sqlite3/introspection.py @@ -57,9 +57,9 @@ class DatabaseIntrospection(BaseDatabaseIntrospection): def get_field_type(self, data_type, description): field_type = super().get_field_type(data_type, description) - if description.pk and field_type in {'BigIntegerField', 'IntegerField'}: - # No support for BigAutoField as SQLite treats all integer primary - # keys as signed 64-bit integers. + if description.pk and field_type in {'BigIntegerField', 'IntegerField', 'SmallIntegerField'}: + # No support for BigAutoField or SmallAutoField as SQLite treats + # all integer primary keys as signed 64-bit integers. return 'AutoField' return field_type diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index 1388dffc58..ff686e4f62 100644 --- a/django/db/models/fields/__init__.py +++ b/django/db/models/fields/__init__.py @@ -38,8 +38,8 @@ __all__ = [ 'EmailField', 'Empty', 'Field', 'FieldDoesNotExist', 'FilePathField', 'FloatField', 'GenericIPAddressField', 'IPAddressField', 'IntegerField', 'NOT_PROVIDED', 'NullBooleanField', 'PositiveIntegerField', - 'PositiveSmallIntegerField', 'SlugField', 'SmallIntegerField', 'TextField', - 'TimeField', 'URLField', 'UUIDField', + 'PositiveSmallIntegerField', 'SlugField', 'SmallAutoField', + 'SmallIntegerField', 'TextField', 'TimeField', 'URLField', 'UUIDField', ] @@ -985,6 +985,16 @@ class BigAutoField(AutoField): return BigIntegerField().db_type(connection=connection) +class SmallAutoField(AutoField): + description = _('Small integer') + + def get_internal_type(self): + return 'SmallAutoField' + + def rel_db_type(self, connection): + return SmallIntegerField().db_type(connection=connection) + + class BooleanField(Field): empty_strings_allowed = False default_error_messages = { |
