Code

Ticket #14286: 14286-Add_BigAutoField.diff

File 14286-Add_BigAutoField.diff, 7.7 KB (added by mmcnickle, 3 years ago)

Adds BigAutoField as a new field type, with documentation.

Line 
1Index: docs/topics/forms/modelforms.txt
2===================================================================
3--- docs/topics/forms/modelforms.txt    (revision 15481)
4+++ docs/topics/forms/modelforms.txt    (working copy)
5@@ -49,6 +49,8 @@
6     Model field                      Form field
7     ===============================  ========================================
8     ``AutoField``                    Not represented in the form
9+   
10+    ``BigAutoField``                 Not represented in the form
11 
12     ``BigIntegerField``              ``IntegerField`` with ``min_value`` set
13                                      to -9223372036854775808 and ``max_value``
14Index: docs/ref/models/fields.txt
15===================================================================
16--- docs/ref/models/fields.txt  (revision 15481)
17+++ docs/ref/models/fields.txt  (working copy)
18@@ -320,6 +320,20 @@
19 primary key field will automatically be added to your model if you don't specify
20 otherwise. See :ref:`automatic-primary-key-fields`.
21 
22+``BigAutoField``
23+----------------
24+
25+.. versionadded:: trunk
26+
27+.. class:: BigAutoField(**options)
28+
29+Behaves the same as :class:`AutoField` except it uses a :class:`BigIntegerField`
30+integer as the field. Use this field if you require a larger number of IDs than
31+:class:`AutoField` allows.
32+
33+You may only have one instance of :class:`AutoField` or :class:`BigAutoField`
34+in your model.
35+
36 ``BigIntegerField``
37 -------------------
38 
39Index: django/db/models/fields/__init__.py
40===================================================================
41--- django/db/models/fields/__init__.py (revision 15481)
42+++ django/db/models/fields/__init__.py (working copy)
43@@ -487,6 +487,12 @@
44     def formfield(self, **kwargs):
45         return None
46 
47+class BigAutoField(AutoField):
48+    description = _("Big (8 byte) integer")
49+   
50+    def get_internal_type(self):
51+        return "BigAutoField"
52+
53 class BooleanField(Field):
54     empty_strings_allowed = False
55     default_error_messages = {
56Index: django/db/models/fields/related.py
57===================================================================
58--- django/db/models/fields/related.py  (revision 15481)
59+++ django/db/models/fields/related.py  (working copy)
60@@ -2,8 +2,9 @@
61 from django.db import connection, router, transaction
62 from django.db.backends import util
63 from django.db.models import signals, get_model
64-from django.db.models.fields import (AutoField, Field, IntegerField,
65-    PositiveIntegerField, PositiveSmallIntegerField, FieldDoesNotExist)
66+from django.db.models.fields import (AutoField, BigAutoField, Field,
67+    BigIntegerField, IntegerField, PositiveIntegerField,
68+    PositiveSmallIntegerField, FieldDoesNotExist)
69 from django.db.models.related import RelatedObject
70 from django.db.models.query import QuerySet
71 from django.db.models.query_utils import QueryWrapper
72@@ -919,11 +920,16 @@
73     def db_type(self, connection):
74         # The database column type of a ForeignKey is the column type
75         # of the field to which it points. An exception is if the ForeignKey
76-        # points to an AutoField/PositiveIntegerField/PositiveSmallIntegerField,
77-        # in which case the column type is simply that of an IntegerField.
78+        # points to an AutoField/BigAutoField/PositiveIntegerField/
79+        # PositiveSmallIntegerField,
80+        # in which case the column type is simply that of an IntegerField
81+        # (or BigIntegerField in the case of BigAutoField).
82         # If the database needs similar types for key fields however, the only
83-        # thing we can do is making AutoField an IntegerField.
84+        # thing we can do is to make AutoField an IntegerField
85+        # or BigAutoField a BigIntegerField
86         rel_field = self.rel.get_related_field()
87+        if isinstance(rel_field, BigAutoField):
88+            return BigIntegerField().db_type(connection=connection)
89         if (isinstance(rel_field, AutoField) or
90                 (not connection.features.related_fields_match_type and
91                 isinstance(rel_field, (PositiveIntegerField,
92Index: django/db/backends/postgresql/creation.py
93===================================================================
94--- django/db/backends/postgresql/creation.py   (revision 15481)
95+++ django/db/backends/postgresql/creation.py   (working copy)
96@@ -8,6 +8,7 @@
97     # If a column type is set to None, it won't be included in the output.
98     data_types = {
99         'AutoField':         'serial',
100+        'BigAutoField':      'bigserial',
101         'BooleanField':      'boolean',
102         'CharField':         'varchar(%(max_length)s)',
103         'CommaSeparatedIntegerField': 'varchar(%(max_length)s)',
104Index: django/db/backends/sqlite3/base.py
105===================================================================
106--- django/db/backends/sqlite3/base.py  (revision 15481)
107+++ django/db/backends/sqlite3/base.py  (working copy)
108@@ -149,7 +149,7 @@
109         internal_type = field.get_internal_type()
110         if internal_type == 'DecimalField':
111             return util.typecast_decimal(field.format_number(value))
112-        elif internal_type and internal_type.endswith('IntegerField') or internal_type == 'AutoField':
113+        elif internal_type and internal_type.endswith('IntegerField') or internal_type.endswith('AutoField'):
114             return int(value)
115         elif internal_type == 'DateField':
116             return util.typecast_date(value)
117Index: django/db/backends/sqlite3/creation.py
118===================================================================
119--- django/db/backends/sqlite3/creation.py      (revision 15481)
120+++ django/db/backends/sqlite3/creation.py      (working copy)
121@@ -8,6 +8,7 @@
122     # schema inspection is more useful.
123     data_types = {
124         'AutoField':                    'integer',
125+        'BigAutoField':                 'bigint',
126         'BooleanField':                 'bool',
127         'CharField':                    'varchar(%(max_length)s)',
128         'CommaSeparatedIntegerField':   'varchar(%(max_length)s)',
129Index: django/db/backends/mysql/creation.py
130===================================================================
131--- django/db/backends/mysql/creation.py        (revision 15481)
132+++ django/db/backends/mysql/creation.py        (working copy)
133@@ -7,6 +7,7 @@
134     # If a column type is set to None, it won't be included in the output.
135     data_types = {
136         'AutoField':         'integer AUTO_INCREMENT',
137+        'BigAutoField':      'bigint AUTO_INCREMENT',
138         'BooleanField':      'bool',
139         'CharField':         'varchar(%(max_length)s)',
140         'CommaSeparatedIntegerField': 'varchar(%(max_length)s)',
141Index: django/db/backends/oracle/creation.py
142===================================================================
143--- django/db/backends/oracle/creation.py       (revision 15481)
144+++ django/db/backends/oracle/creation.py       (working copy)
145@@ -15,6 +15,7 @@
146 
147     data_types = {
148         'AutoField':                    'NUMBER(11)',
149+        'BigAutoField':                 'NUMBER(19)',
150         'BooleanField':                 'NUMBER(1) CHECK (%(qn_column)s IN (0,1))',
151         'CharField':                    'NVARCHAR2(%(max_length)s)',
152         'CommaSeparatedIntegerField':   'VARCHAR2(%(max_length)s)',
153Index: django/db/backends/__init__.py
154===================================================================
155--- django/db/backends/__init__.py      (revision 15481)
156+++ django/db/backends/__init__.py      (working copy)
157@@ -565,7 +565,7 @@
158         internal_type = field.get_internal_type()
159         if internal_type == 'DecimalField':
160             return value
161-        elif internal_type and internal_type.endswith('IntegerField') or internal_type == 'AutoField':
162+        elif internal_type and internal_type.endswith('IntegerField') or internal_type.endswith('AutoField'):
163             return int(value)
164         elif internal_type in ('DateField', 'DateTimeField', 'TimeField'):
165             return value