Code

Ticket #14286: bigauto.diff

File bigauto.diff, 9.2 KB (added by jamercee, 16 months ago)

BigAutoField patch file.

Line 
1diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py
2index fe26c98..97d0e68 100644
3--- a/django/db/backends/__init__.py
4+++ b/django/db/backends/__init__.py
5@@ -1055,7 +1055,7 @@ class BaseDatabaseOperations(object):
6         if internal_type == 'FloatField':
7             return float(value)
8         elif (internal_type and (internal_type.endswith('IntegerField')
9-                                 or internal_type == 'AutoField')):
10+                                 or internal_type.endswith('AutoField'))):
11             return int(value)
12         return value
13 
14diff --git a/django/db/backends/mysql/creation.py b/django/db/backends/mysql/creation.py
15index 3a57c29..9dfc641 100644
16--- a/django/db/backends/mysql/creation.py
17+++ b/django/db/backends/mysql/creation.py
18@@ -7,6 +7,7 @@ class DatabaseCreation(BaseDatabaseCreation):
19     # If a column type is set to None, it won't be included in the output.
20     data_types = {
21         'AutoField':         'integer AUTO_INCREMENT',
22+        'BigAutoField':      'bigint AUTO_INCREMENT',
23         'BinaryField':       'longblob',
24         'BooleanField':      'bool',
25         'CharField':         'varchar(%(max_length)s)',
26diff --git a/django/db/backends/oracle/creation.py b/django/db/backends/oracle/creation.py
27index aaca74e..dd3ddb3 100644
28--- a/django/db/backends/oracle/creation.py
29+++ b/django/db/backends/oracle/creation.py
30@@ -17,6 +17,7 @@ class DatabaseCreation(BaseDatabaseCreation):
31 
32     data_types = {
33         'AutoField':                    'NUMBER(11)',
34+        'BigAutoField':                 'NUMBER(19)',
35         'BinaryField':                  'BLOB',
36         'BooleanField':                 'NUMBER(1) CHECK (%(qn_column)s IN (0,1))',
37         'CharField':                    'NVARCHAR2(%(max_length)s)',
38diff --git a/django/db/backends/postgresql_psycopg2/creation.py b/django/db/backends/postgresql_psycopg2/creation.py
39index b19926b..b745ef8 100644
40--- a/django/db/backends/postgresql_psycopg2/creation.py
41+++ b/django/db/backends/postgresql_psycopg2/creation.py
42@@ -11,6 +11,7 @@ class DatabaseCreation(BaseDatabaseCreation):
43     # If a column type is set to None, it won't be included in the output.
44     data_types = {
45         'AutoField':         'serial',
46+        'BigAutoField':      'bigserial',
47         'BinaryField':       'bytea',
48         'BooleanField':      'boolean',
49         'CharField':         'varchar(%(max_length)s)',
50diff --git a/django/db/backends/sqlite3/base.py b/django/db/backends/sqlite3/base.py
51index 416a629..d96cdfe 100644
52--- a/django/db/backends/sqlite3/base.py
53+++ b/django/db/backends/sqlite3/base.py
54@@ -248,7 +248,8 @@ class DatabaseOperations(BaseDatabaseOperations):
55         internal_type = field.get_internal_type()
56         if internal_type == 'DecimalField':
57             return util.typecast_decimal(field.format_number(value))
58-        elif internal_type and internal_type.endswith('IntegerField') or internal_type == 'AutoField':
59+        elif (internal_type and internal_type.endswith('IntegerField') or
60+                internal_type.endswith('AutoField')):
61             return int(value)
62         elif internal_type == 'DateField':
63             return parse_date(value)
64diff --git a/django/db/backends/sqlite3/creation.py b/django/db/backends/sqlite3/creation.py
65index c90a697..6132b10 100644
66--- a/django/db/backends/sqlite3/creation.py
67+++ b/django/db/backends/sqlite3/creation.py
68@@ -9,6 +9,7 @@ class DatabaseCreation(BaseDatabaseCreation):
69     # schema inspection is more useful.
70     data_types = {
71         'AutoField':                    'integer',
72+        'BigAutoField':                 'integer',
73         'BinaryField':                  'BLOB',
74         'BooleanField':                 'bool',
75         'CharField':                    'varchar(%(max_length)s)',
76diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
77index f9f913b..8b9f9ed 100644
78--- a/django/db/models/fields/__init__.py
79+++ b/django/db/models/fields/__init__.py
80@@ -215,6 +215,13 @@ class Field(object):
81         self.run_validators(value)
82         return value
83 
84+    def _internal_to_db_type(self, internal_type, connection):
85+        data = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_")
86+        try:
87+            return connection.creation.data_types[internal_type] % data
88+        except KeyError:
89+            return None
90+
91     def db_type(self, connection):
92         """
93         Returns the database column data type for this field, for the provided
94@@ -235,12 +242,14 @@ class Field(object):
95         # mapped to one of the built-in Django field types. In this case, you
96         # can implement db_type() instead of get_internal_type() to specify
97         # exactly which wacky database column type you want to use.
98-        data = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_")
99-        try:
100-            return (connection.creation.data_types[self.get_internal_type()]
101-                    % data)
102-        except KeyError:
103-            return None
104+        return self._internal_to_db_type(self.get_internal_type(), connection)
105+
106+    def rel_db_type(self, connection):
107+        """
108+        Returns the database column data type for related field
109+        referencing to this.
110+        """
111+        return self.db_type(connection)
112 
113     @property
114     def unique(self):
115@@ -530,14 +539,19 @@ class AutoField(Field):
116         'invalid': _("'%s' value must be an integer."),
117     }
118 
119-    def __init__(self, *args, **kwargs):
120+    def __init__(self, verbose_name=None, name=None, big=False, **kwargs):
121         assert kwargs.get('primary_key', False) is True, \
122                "%ss must have primary_key=True." % self.__class__.__name__
123         kwargs['blank'] = True
124-        Field.__init__(self, *args, **kwargs)
125+        self.big = big
126+        Field.__init__(self, verbose_name, name, **kwargs)
127 
128     def get_internal_type(self):
129-        return "AutoField"
130+        return "AutoField" if not self.big else 'BigAutoField'
131+
132+    def rel_db_type(self, connection):
133+        db_type = 'IntegerField' if not self.big else 'BigIntegerField'
134+        return self._internal_to_db_type(db_type, connection)
135 
136     def to_python(self, value):
137         if value is None:
138@@ -1152,6 +1166,11 @@ class PositiveIntegerField(IntegerField):
139     def get_internal_type(self):
140         return "PositiveIntegerField"
141 
142+    def rel_db_type(self, connection):
143+        if connection.features.related_fields_match_type:
144+            return self.db_type(connection)
145+        return self._internal_to_db_type('IntegerField', connection)
146+
147     def formfield(self, **kwargs):
148         defaults = {'min_value': 0}
149         defaults.update(kwargs)
150@@ -1163,6 +1182,11 @@ class PositiveSmallIntegerField(IntegerField):
151     def get_internal_type(self):
152         return "PositiveSmallIntegerField"
153 
154+    def rel_db_type(self, connection):
155+        if connection.features.related_fields_match_type:
156+            return self.db_type(connection)
157+        return self._internal_to_db_type('IntegerField', connection)
158+
159     def formfield(self, **kwargs):
160         defaults = {'min_value': 0}
161         defaults.update(kwargs)
162diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
163index 01b8a55..071a5a9 100644
164--- a/django/db/models/fields/related.py
165+++ b/django/db/models/fields/related.py
166@@ -1113,19 +1113,8 @@ class ForeignKey(RelatedField, Field):
167         return super(ForeignKey, self).formfield(**defaults)
168 
169     def db_type(self, connection):
170-        # The database column type of a ForeignKey is the column type
171-        # of the field to which it points. An exception is if the ForeignKey
172-        # points to an AutoField/PositiveIntegerField/PositiveSmallIntegerField,
173-        # in which case the column type is simply that of an IntegerField.
174-        # If the database needs similar types for key fields however, the only
175-        # thing we can do is making AutoField an IntegerField.
176         rel_field = self.rel.get_related_field()
177-        if (isinstance(rel_field, AutoField) or
178-                (not connection.features.related_fields_match_type and
179-                isinstance(rel_field, (PositiveIntegerField,
180-                                       PositiveSmallIntegerField)))):
181-            return IntegerField().db_type(connection=connection)
182-        return rel_field.db_type(connection=connection)
183+        return rel_field.rel_db_type(connection=connection)
184 
185 
186 class OneToOneField(ForeignKey):
187diff --git a/docs/ref/models/fields.txt b/docs/ref/models/fields.txt
188index 1dbc8c3..d00e1f2 100644
189--- a/docs/ref/models/fields.txt
190+++ b/docs/ref/models/fields.txt
191@@ -331,13 +331,22 @@ Field types
192 ``AutoField``
193 -------------
194 
195-.. class:: AutoField(**options)
196+.. class:: AutoField([big=False, **options]
197 
198 An :class:`IntegerField` that automatically increments
199 according to available IDs. You usually won't need to use this directly; a
200 primary key field will automatically be added to your model if you don't specify
201 otherwise. See :ref:`automatic-primary-key-fields`.
202 
203+.. attribute:: AutoField.big
204+
205+.. versionadded:: 1.5
206+
207+Optional.  Either ``False`` or ``True``.  Default is ``False``. Allow you
208+to use bigint for storing field values. This extends values range up to
209+max 64 bit integer (from 1 to 9223372036854775807).
210+
211+
212 ``BigIntegerField``
213 -------------------
214