diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py
index 2762350..2c56ef6 100644
a
|
b
|
class BaseDatabaseOperations(object):
|
847 | 847 | internal_type = field.get_internal_type() |
848 | 848 | if internal_type == 'DecimalField': |
849 | 849 | return value |
850 | | elif internal_type and internal_type.endswith('IntegerField') or internal_type == 'AutoField': |
| 850 | elif (internal_type and internal_type.endswith('IntegerField') |
| 851 | or internal_type.endswith('AutoField')): |
851 | 852 | return int(value) |
852 | 853 | elif internal_type in ('DateField', 'DateTimeField', 'TimeField'): |
853 | 854 | return value |
diff --git a/django/db/backends/mysql/creation.py b/django/db/backends/mysql/creation.py
index 53bd57e..9876278 100644
a
|
b
|
class DatabaseCreation(BaseDatabaseCreation):
|
6 | 6 | # be interpolated against the values of Field.__dict__ before being output. |
7 | 7 | # If a column type is set to None, it won't be included in the output. |
8 | 8 | data_types = { |
| 9 | 'BigAutoField': 'bigint AUTO_INCREMENT', |
9 | 10 | 'AutoField': 'integer AUTO_INCREMENT', |
10 | 11 | 'BooleanField': 'bool', |
11 | 12 | 'CharField': 'varchar(%(max_length)s)', |
diff --git a/django/db/backends/oracle/creation.py b/django/db/backends/oracle/creation.py
index 2f096f7..7e97271 100644
a
|
b
|
class DatabaseCreation(BaseDatabaseCreation):
|
15 | 15 | # output (the "qn_" prefix is stripped before the lookup is performed. |
16 | 16 | |
17 | 17 | data_types = { |
| 18 | 'BigAutoField': 'NUMBER(11)', |
18 | 19 | 'AutoField': 'NUMBER(11)', |
19 | 20 | 'BooleanField': 'NUMBER(1) CHECK (%(qn_column)s IN (0,1))', |
20 | 21 | 'CharField': 'NVARCHAR2(%(max_length)s)', |
diff --git a/django/db/backends/postgresql_psycopg2/creation.py b/django/db/backends/postgresql_psycopg2/creation.py
index ca389b9..64223b1 100644
a
|
b
|
class DatabaseCreation(BaseDatabaseCreation):
|
10 | 10 | # be interpolated against the values of Field.__dict__ before being output. |
11 | 11 | # If a column type is set to None, it won't be included in the output. |
12 | 12 | data_types = { |
| 13 | 'BigAutoField': 'bigserial', |
13 | 14 | 'AutoField': 'serial', |
14 | 15 | 'BooleanField': 'boolean', |
15 | 16 | 'CharField': 'varchar(%(max_length)s)', |
diff --git a/django/db/backends/sqlite3/base.py b/django/db/backends/sqlite3/base.py
index 7ce9dd3..5a26344 100644
a
|
b
|
class DatabaseOperations(BaseDatabaseOperations):
|
191 | 191 | internal_type = field.get_internal_type() |
192 | 192 | if internal_type == 'DecimalField': |
193 | 193 | return util.typecast_decimal(field.format_number(value)) |
194 | | elif internal_type and internal_type.endswith('IntegerField') or internal_type == 'AutoField': |
| 194 | elif (internal_type and internal_type.endswith('IntegerField') |
| 195 | or internal_type.endswith('AutoField')): |
195 | 196 | return int(value) |
196 | 197 | elif internal_type == 'DateField': |
197 | 198 | return parse_date(value) |
diff --git a/django/db/backends/sqlite3/creation.py b/django/db/backends/sqlite3/creation.py
index efdc457..51ceca9 100644
a
|
b
|
class DatabaseCreation(BaseDatabaseCreation):
|
7 | 7 | # thing" given more verbose field definitions, so leave them as is so that |
8 | 8 | # schema inspection is more useful. |
9 | 9 | data_types = { |
| 10 | 'BigAutoField': 'integer', |
10 | 11 | 'AutoField': 'integer', |
11 | 12 | 'BooleanField': 'bool', |
12 | 13 | 'CharField': 'varchar(%(max_length)s)', |
diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
index f694958..668f367 100644
a
|
b
|
class Field(object):
|
205 | 205 | self.run_validators(value) |
206 | 206 | return value |
207 | 207 | |
| 208 | def _internal_to_db_type(self, internal_type, connection): |
| 209 | data = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_") |
| 210 | try: |
| 211 | return connection.creation.data_types[internal_type] % data |
| 212 | except KeyError: |
| 213 | return None |
| 214 | |
208 | 215 | def db_type(self, connection): |
209 | 216 | """ |
210 | 217 | Returns the database column data type for this field, for the provided |
… |
… |
class Field(object):
|
225 | 232 | # mapped to one of the built-in Django field types. In this case, you |
226 | 233 | # can implement db_type() instead of get_internal_type() to specify |
227 | 234 | # exactly which wacky database column type you want to use. |
228 | | data = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_") |
229 | | try: |
230 | | return (connection.creation.data_types[self.get_internal_type()] |
231 | | % data) |
232 | | except KeyError: |
233 | | return None |
| 235 | return self._internal_to_db_type(self.get_internal_type(), connection) |
| 236 | |
| 237 | def rel_db_type(self, connection): |
| 238 | """ |
| 239 | Returns the database column data type for related field referencing |
| 240 | to this. |
| 241 | """ |
| 242 | return self.db_type(connection) |
234 | 243 | |
235 | 244 | @property |
236 | 245 | def unique(self): |
… |
… |
class AutoField(Field):
|
514 | 523 | 'invalid': _(u"'%s' value must be an integer."), |
515 | 524 | } |
516 | 525 | |
517 | | def __init__(self, *args, **kwargs): |
| 526 | def __init__(self, verbose_name=None, name=None, big=False, **kwargs): |
518 | 527 | assert kwargs.get('primary_key', False) is True, \ |
519 | 528 | "%ss must have primary_key=True." % self.__class__.__name__ |
520 | 529 | kwargs['blank'] = True |
521 | | Field.__init__(self, *args, **kwargs) |
| 530 | self.big = big |
| 531 | Field.__init__(self, verbose_name, name, **kwargs) |
522 | 532 | |
523 | 533 | def get_internal_type(self): |
524 | | return "AutoField" |
| 534 | return 'AutoField' if not self.big else 'BigAutoField' |
| 535 | |
| 536 | def rel_db_type(self, connection): |
| 537 | db_type = 'IntegerField' if not self.big else 'BigIntegerField' |
| 538 | return self._internal_to_db_type(db_type, connection) |
525 | 539 | |
526 | 540 | def to_python(self, value): |
527 | 541 | if value is None: |
… |
… |
class PositiveIntegerField(IntegerField):
|
1139 | 1153 | def get_internal_type(self): |
1140 | 1154 | return "PositiveIntegerField" |
1141 | 1155 | |
| 1156 | def rel_db_type(self, connection): |
| 1157 | if connection.features.related_fields_match_type: |
| 1158 | return self.db_type(connection) |
| 1159 | return self._internal_to_db_type('IntegerField', connection) |
| 1160 | |
1142 | 1161 | def formfield(self, **kwargs): |
1143 | 1162 | defaults = {'min_value': 0} |
1144 | 1163 | defaults.update(kwargs) |
… |
… |
class PositiveSmallIntegerField(IntegerField):
|
1150 | 1169 | def get_internal_type(self): |
1151 | 1170 | return "PositiveSmallIntegerField" |
1152 | 1171 | |
| 1172 | def rel_db_type(self, connection): |
| 1173 | if connection.features.related_fields_match_type: |
| 1174 | return self.db_type(connection) |
| 1175 | return self._internal_to_db_type('IntegerField', connection) |
| 1176 | |
1153 | 1177 | def formfield(self, **kwargs): |
1154 | 1178 | defaults = {'min_value': 0} |
1155 | 1179 | defaults.update(kwargs) |
diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
index a16f955..0783590 100644
a
|
b
|
class ForeignKey(RelatedField, Field):
|
1018 | 1018 | return super(ForeignKey, self).formfield(**defaults) |
1019 | 1019 | |
1020 | 1020 | def db_type(self, connection): |
1021 | | # The database column type of a ForeignKey is the column type |
1022 | | # of the field to which it points. An exception is if the ForeignKey |
1023 | | # points to an AutoField/PositiveIntegerField/PositiveSmallIntegerField, |
1024 | | # in which case the column type is simply that of an IntegerField. |
1025 | | # If the database needs similar types for key fields however, the only |
1026 | | # thing we can do is making AutoField an IntegerField. |
1027 | 1021 | rel_field = self.rel.get_related_field() |
1028 | | if (isinstance(rel_field, AutoField) or |
1029 | | (not connection.features.related_fields_match_type and |
1030 | | isinstance(rel_field, (PositiveIntegerField, |
1031 | | PositiveSmallIntegerField)))): |
1032 | | return IntegerField().db_type(connection=connection) |
1033 | | return rel_field.db_type(connection=connection) |
| 1022 | return rel_field.rel_db_type(connection=connection) |
1034 | 1023 | |
1035 | 1024 | class OneToOneField(ForeignKey): |
1036 | 1025 | """ |
diff --git a/docs/ref/models/fields.txt b/docs/ref/models/fields.txt
index eab7ad9..d88368a 100644
a
|
b
|
Field types
|
320 | 320 | ``AutoField`` |
321 | 321 | ------------- |
322 | 322 | |
323 | | .. class:: AutoField(**options) |
| 323 | .. class:: AutoField([big=False, **options]) |
324 | 324 | |
325 | 325 | An :class:`IntegerField` that automatically increments |
326 | 326 | according to available IDs. You usually won't need to use this directly; a |
327 | 327 | primary key field will automatically be added to your model if you don't specify |
328 | 328 | otherwise. See :ref:`automatic-primary-key-fields`. |
329 | 329 | |
| 330 | .. attribute:: AutoField.big |
| 331 | |
| 332 | .. versionadded:: 1.5 |
| 333 | |
| 334 | Optional. Either ``False`` or ``True``. Default is ``False``. Allow you |
| 335 | to use bigint for storing field values. This extends values range up to |
| 336 | max 64 bit integer (from 1 to 9223372036854775807). |
| 337 | |
| 338 | |
330 | 339 | ``BigIntegerField`` |
331 | 340 | ------------------- |
332 | 341 | |