diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py
index fe26c98..97d0e68 100644
a
|
b
|
class BaseDatabaseOperations(object):
|
1055 | 1055 | if internal_type == 'FloatField': |
1056 | 1056 | return float(value) |
1057 | 1057 | elif (internal_type and (internal_type.endswith('IntegerField') |
1058 | | or internal_type == 'AutoField')): |
| 1058 | or internal_type.endswith('AutoField'))): |
1059 | 1059 | return int(value) |
1060 | 1060 | return value |
1061 | 1061 | |
diff --git a/django/db/backends/mysql/creation.py b/django/db/backends/mysql/creation.py
index 3a57c29..9dfc641 100644
a
|
b
|
class DatabaseCreation(BaseDatabaseCreation):
|
7 | 7 | # If a column type is set to None, it won't be included in the output. |
8 | 8 | data_types = { |
9 | 9 | 'AutoField': 'integer AUTO_INCREMENT', |
| 10 | 'BigAutoField': 'bigint AUTO_INCREMENT', |
10 | 11 | 'BinaryField': 'longblob', |
11 | 12 | 'BooleanField': 'bool', |
12 | 13 | 'CharField': 'varchar(%(max_length)s)', |
diff --git a/django/db/backends/oracle/creation.py b/django/db/backends/oracle/creation.py
index aaca74e..dd3ddb3 100644
a
|
b
|
class DatabaseCreation(BaseDatabaseCreation):
|
17 | 17 | |
18 | 18 | data_types = { |
19 | 19 | 'AutoField': 'NUMBER(11)', |
| 20 | 'BigAutoField': 'NUMBER(19)', |
20 | 21 | 'BinaryField': 'BLOB', |
21 | 22 | 'BooleanField': 'NUMBER(1) CHECK (%(qn_column)s IN (0,1))', |
22 | 23 | 'CharField': 'NVARCHAR2(%(max_length)s)', |
diff --git a/django/db/backends/postgresql_psycopg2/creation.py b/django/db/backends/postgresql_psycopg2/creation.py
index b19926b..b745ef8 100644
a
|
b
|
class DatabaseCreation(BaseDatabaseCreation):
|
11 | 11 | # If a column type is set to None, it won't be included in the output. |
12 | 12 | data_types = { |
13 | 13 | 'AutoField': 'serial', |
| 14 | 'BigAutoField': 'bigserial', |
14 | 15 | 'BinaryField': 'bytea', |
15 | 16 | 'BooleanField': 'boolean', |
16 | 17 | 'CharField': 'varchar(%(max_length)s)', |
diff --git a/django/db/backends/sqlite3/base.py b/django/db/backends/sqlite3/base.py
index 416a629..d96cdfe 100644
a
|
b
|
class DatabaseOperations(BaseDatabaseOperations):
|
248 | 248 | internal_type = field.get_internal_type() |
249 | 249 | if internal_type == 'DecimalField': |
250 | 250 | return util.typecast_decimal(field.format_number(value)) |
251 | | elif internal_type and internal_type.endswith('IntegerField') or internal_type == 'AutoField': |
| 251 | elif (internal_type and internal_type.endswith('IntegerField') or |
| 252 | internal_type.endswith('AutoField')): |
252 | 253 | return int(value) |
253 | 254 | elif internal_type == 'DateField': |
254 | 255 | return parse_date(value) |
diff --git a/django/db/backends/sqlite3/creation.py b/django/db/backends/sqlite3/creation.py
index c90a697..6132b10 100644
a
|
b
|
class DatabaseCreation(BaseDatabaseCreation):
|
9 | 9 | # schema inspection is more useful. |
10 | 10 | data_types = { |
11 | 11 | 'AutoField': 'integer', |
| 12 | 'BigAutoField': 'integer', |
12 | 13 | 'BinaryField': 'BLOB', |
13 | 14 | 'BooleanField': 'bool', |
14 | 15 | 'CharField': 'varchar(%(max_length)s)', |
diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
index f9f913b..8b9f9ed 100644
a
|
b
|
class Field(object):
|
215 | 215 | self.run_validators(value) |
216 | 216 | return value |
217 | 217 | |
| 218 | def _internal_to_db_type(self, internal_type, connection): |
| 219 | data = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_") |
| 220 | try: |
| 221 | return connection.creation.data_types[internal_type] % data |
| 222 | except KeyError: |
| 223 | return None |
| 224 | |
218 | 225 | def db_type(self, connection): |
219 | 226 | """ |
220 | 227 | Returns the database column data type for this field, for the provided |
… |
… |
class Field(object):
|
235 | 242 | # mapped to one of the built-in Django field types. In this case, you |
236 | 243 | # can implement db_type() instead of get_internal_type() to specify |
237 | 244 | # exactly which wacky database column type you want to use. |
238 | | data = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_") |
239 | | try: |
240 | | return (connection.creation.data_types[self.get_internal_type()] |
241 | | % data) |
242 | | except KeyError: |
243 | | return None |
| 245 | return self._internal_to_db_type(self.get_internal_type(), connection) |
| 246 | |
| 247 | def rel_db_type(self, connection): |
| 248 | """ |
| 249 | Returns the database column data type for related field |
| 250 | referencing to this. |
| 251 | """ |
| 252 | return self.db_type(connection) |
244 | 253 | |
245 | 254 | @property |
246 | 255 | def unique(self): |
… |
… |
class AutoField(Field):
|
530 | 539 | 'invalid': _("'%s' value must be an integer."), |
531 | 540 | } |
532 | 541 | |
533 | | def __init__(self, *args, **kwargs): |
| 542 | def __init__(self, verbose_name=None, name=None, big=False, **kwargs): |
534 | 543 | assert kwargs.get('primary_key', False) is True, \ |
535 | 544 | "%ss must have primary_key=True." % self.__class__.__name__ |
536 | 545 | kwargs['blank'] = True |
537 | | Field.__init__(self, *args, **kwargs) |
| 546 | self.big = big |
| 547 | Field.__init__(self, verbose_name, name, **kwargs) |
538 | 548 | |
539 | 549 | def get_internal_type(self): |
540 | | return "AutoField" |
| 550 | return "AutoField" if not self.big else 'BigAutoField' |
| 551 | |
| 552 | def rel_db_type(self, connection): |
| 553 | db_type = 'IntegerField' if not self.big else 'BigIntegerField' |
| 554 | return self._internal_to_db_type(db_type, connection) |
541 | 555 | |
542 | 556 | def to_python(self, value): |
543 | 557 | if value is None: |
… |
… |
class PositiveIntegerField(IntegerField):
|
1152 | 1166 | def get_internal_type(self): |
1153 | 1167 | return "PositiveIntegerField" |
1154 | 1168 | |
| 1169 | def rel_db_type(self, connection): |
| 1170 | if connection.features.related_fields_match_type: |
| 1171 | return self.db_type(connection) |
| 1172 | return self._internal_to_db_type('IntegerField', connection) |
| 1173 | |
1155 | 1174 | def formfield(self, **kwargs): |
1156 | 1175 | defaults = {'min_value': 0} |
1157 | 1176 | defaults.update(kwargs) |
… |
… |
class PositiveSmallIntegerField(IntegerField):
|
1163 | 1182 | def get_internal_type(self): |
1164 | 1183 | return "PositiveSmallIntegerField" |
1165 | 1184 | |
| 1185 | def rel_db_type(self, connection): |
| 1186 | if connection.features.related_fields_match_type: |
| 1187 | return self.db_type(connection) |
| 1188 | return self._internal_to_db_type('IntegerField', connection) |
| 1189 | |
1166 | 1190 | def formfield(self, **kwargs): |
1167 | 1191 | defaults = {'min_value': 0} |
1168 | 1192 | defaults.update(kwargs) |
diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
index 01b8a55..071a5a9 100644
a
|
b
|
class ForeignKey(RelatedField, Field):
|
1113 | 1113 | return super(ForeignKey, self).formfield(**defaults) |
1114 | 1114 | |
1115 | 1115 | def db_type(self, connection): |
1116 | | # The database column type of a ForeignKey is the column type |
1117 | | # of the field to which it points. An exception is if the ForeignKey |
1118 | | # points to an AutoField/PositiveIntegerField/PositiveSmallIntegerField, |
1119 | | # in which case the column type is simply that of an IntegerField. |
1120 | | # If the database needs similar types for key fields however, the only |
1121 | | # thing we can do is making AutoField an IntegerField. |
1122 | 1116 | rel_field = self.rel.get_related_field() |
1123 | | if (isinstance(rel_field, AutoField) or |
1124 | | (not connection.features.related_fields_match_type and |
1125 | | isinstance(rel_field, (PositiveIntegerField, |
1126 | | PositiveSmallIntegerField)))): |
1127 | | return IntegerField().db_type(connection=connection) |
1128 | | return rel_field.db_type(connection=connection) |
| 1117 | return rel_field.rel_db_type(connection=connection) |
1129 | 1118 | |
1130 | 1119 | |
1131 | 1120 | class OneToOneField(ForeignKey): |
diff --git a/docs/ref/models/fields.txt b/docs/ref/models/fields.txt
index 1dbc8c3..d00e1f2 100644
a
|
b
|
Field types
|
331 | 331 | ``AutoField`` |
332 | 332 | ------------- |
333 | 333 | |
334 | | .. class:: AutoField(**options) |
| 334 | .. class:: AutoField([big=False, **options] |
335 | 335 | |
336 | 336 | An :class:`IntegerField` that automatically increments |
337 | 337 | according to available IDs. You usually won't need to use this directly; a |
338 | 338 | primary key field will automatically be added to your model if you don't specify |
339 | 339 | otherwise. See :ref:`automatic-primary-key-fields`. |
340 | 340 | |
| 341 | .. attribute:: AutoField.big |
| 342 | |
| 343 | .. versionadded:: 1.5 |
| 344 | |
| 345 | Optional. Either ``False`` or ``True``. Default is ``False``. Allow you |
| 346 | to use bigint for storing field values. This extends values range up to |
| 347 | max 64 bit integer (from 1 to 9223372036854775807). |
| 348 | |
| 349 | |
341 | 350 | ``BigIntegerField`` |
342 | 351 | ------------------- |
343 | 352 | |