diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py
index 7674f5c..1e9cca2 100644
a
|
b
|
class BaseDatabaseOperations(object):
|
831 | 831 | internal_type = field.get_internal_type() |
832 | 832 | if internal_type == 'DecimalField': |
833 | 833 | return value |
834 | | elif internal_type and internal_type.endswith('IntegerField') or internal_type == 'AutoField': |
| 834 | elif (internal_type and internal_type.endswith('IntegerField') |
| 835 | or internal_type.endswith('AutoField')): |
835 | 836 | return int(value) |
836 | 837 | elif internal_type in ('DateField', 'DateTimeField', 'TimeField'): |
837 | 838 | return value |
diff --git a/django/db/backends/mysql/creation.py b/django/db/backends/mysql/creation.py
index 53bd57e..86164ce 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 | 'UnsignedAutoField': 'integer unsigned 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 44f8d91..dc38f4e 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 | 'UnsignedAutoField': '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..30aef55 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 | 'UnsignedAutoField': '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 f5f0c64..3a97408 100644
a
|
b
|
class DatabaseOperations(BaseDatabaseOperations):
|
178 | 178 | internal_type = field.get_internal_type() |
179 | 179 | if internal_type == 'DecimalField': |
180 | 180 | return util.typecast_decimal(field.format_number(value)) |
181 | | elif internal_type and internal_type.endswith('IntegerField') or internal_type == 'AutoField': |
| 181 | elif (internal_type and internal_type.endswith('IntegerField') |
| 182 | or internal_type.endswith('AutoField')): |
182 | 183 | return int(value) |
183 | 184 | elif internal_type == 'DateField': |
184 | 185 | return parse_date(value) |
diff --git a/django/db/backends/sqlite3/creation.py b/django/db/backends/sqlite3/creation.py
index cd39356..2e9429c 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 | 'UnsignedAutoField': '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 22546c2..aee84cb 100644
a
|
b
|
class Field(object):
|
201 | 201 | self.run_validators(value) |
202 | 202 | return value |
203 | 203 | |
| 204 | def _internal_to_db_type(self, internal_type, connection): |
| 205 | data = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_") |
| 206 | try: |
| 207 | return connection.creation.data_types[internal_type] % data |
| 208 | except KeyError: |
| 209 | return None |
| 210 | |
204 | 211 | def db_type(self, connection): |
205 | 212 | """ |
206 | 213 | Returns the database column data type for this field, for the provided |
… |
… |
class Field(object):
|
221 | 228 | # mapped to one of the built-in Django field types. In this case, you |
222 | 229 | # can implement db_type() instead of get_internal_type() to specify |
223 | 230 | # exactly which wacky database column type you want to use. |
224 | | data = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_") |
225 | | try: |
226 | | return (connection.creation.data_types[self.get_internal_type()] |
227 | | % data) |
228 | | except KeyError: |
229 | | return None |
| 231 | return self._internal_to_db_type(self.get_internal_type(), connection) |
| 232 | |
| 233 | def rel_db_type(self, connection): |
| 234 | """ |
| 235 | Returns the database column data type for related field referencing |
| 236 | to this. |
| 237 | """ |
| 238 | return self.db_type(connection) |
230 | 239 | |
231 | 240 | @property |
232 | 241 | def unique(self): |
… |
… |
class AutoField(Field):
|
510 | 519 | 'invalid': _(u"'%s' value must be an integer."), |
511 | 520 | } |
512 | 521 | |
513 | | def __init__(self, *args, **kwargs): |
| 522 | def __init__(self, verbose_name=None, name=None, unsigned=False, **kwargs): |
514 | 523 | assert kwargs.get('primary_key', False) is True, \ |
515 | 524 | "%ss must have primary_key=True." % self.__class__.__name__ |
516 | 525 | kwargs['blank'] = True |
517 | | Field.__init__(self, *args, **kwargs) |
| 526 | self.unsigned = unsigned |
| 527 | Field.__init__(self, verbose_name, name, **kwargs) |
518 | 528 | |
519 | 529 | def get_internal_type(self): |
520 | | return "AutoField" |
| 530 | return 'AutoField' if not self.unsigned else 'UnsignedAutoField' |
| 531 | |
| 532 | def rel_db_type(self, connection): |
| 533 | db_type = 'IntegerField' if not self.unsigned \ |
| 534 | else 'PositiveIntegerField' |
| 535 | return self._internal_to_db_type(db_type, connection) |
521 | 536 | |
522 | 537 | def to_python(self, value): |
523 | 538 | if value is None: |
… |
… |
class PositiveIntegerField(IntegerField):
|
1118 | 1133 | def get_internal_type(self): |
1119 | 1134 | return "PositiveIntegerField" |
1120 | 1135 | |
| 1136 | def rel_db_type(self, connection): |
| 1137 | if connection.features.related_fields_match_type: |
| 1138 | return self.db_type(connection) |
| 1139 | return self._internal_to_db_type('IntegerField', connection) |
| 1140 | |
1121 | 1141 | def formfield(self, **kwargs): |
1122 | 1142 | defaults = {'min_value': 0} |
1123 | 1143 | defaults.update(kwargs) |
… |
… |
class PositiveSmallIntegerField(IntegerField):
|
1129 | 1149 | def get_internal_type(self): |
1130 | 1150 | return "PositiveSmallIntegerField" |
1131 | 1151 | |
| 1152 | def rel_db_type(self, connection): |
| 1153 | if connection.features.related_fields_match_type: |
| 1154 | return self.db_type(connection) |
| 1155 | return self._internal_to_db_type('IntegerField', connection) |
| 1156 | |
1132 | 1157 | def formfield(self, **kwargs): |
1133 | 1158 | defaults = {'min_value': 0} |
1134 | 1159 | defaults.update(kwargs) |
diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
index 53f9b9f..7dcdb7d 100644
a
|
b
|
class ForeignKey(RelatedField, Field):
|
1008 | 1008 | return super(ForeignKey, self).formfield(**defaults) |
1009 | 1009 | |
1010 | 1010 | def db_type(self, connection): |
1011 | | # The database column type of a ForeignKey is the column type |
1012 | | # of the field to which it points. An exception is if the ForeignKey |
1013 | | # points to an AutoField/PositiveIntegerField/PositiveSmallIntegerField, |
1014 | | # in which case the column type is simply that of an IntegerField. |
1015 | | # If the database needs similar types for key fields however, the only |
1016 | | # thing we can do is making AutoField an IntegerField. |
1017 | 1011 | rel_field = self.rel.get_related_field() |
1018 | | if (isinstance(rel_field, AutoField) or |
1019 | | (not connection.features.related_fields_match_type and |
1020 | | isinstance(rel_field, (PositiveIntegerField, |
1021 | | PositiveSmallIntegerField)))): |
1022 | | return IntegerField().db_type(connection=connection) |
1023 | | return rel_field.db_type(connection=connection) |
| 1012 | return rel_field.rel_db_type(connection=connection) |
1024 | 1013 | |
1025 | 1014 | class OneToOneField(ForeignKey): |
1026 | 1015 | """ |