Ticket #56: 56_bigint_new.diff
File 56_bigint_new.diff, 13.1 KB (added by , 12 years ago) |
---|
-
django/db/backends/__init__.py
diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py index 649f807..3802b1c 100644
a b class BaseDatabaseOperations(object): 889 889 return value 890 890 elif internal_type == 'FloatField': 891 891 return float(value) 892 elif (internal_type and (internal_type.endswith('IntegerField')893 or internal_type =='AutoField')):892 elif (internal_type and internal_type.endswith('IntegerField') 893 or internal_type.endswith('AutoField')): 894 894 return int(value) 895 895 elif internal_type in ('DateField', 'DateTimeField', 'TimeField'): 896 896 return value -
django/db/backends/mysql/base.py
diff --git a/django/db/backends/mysql/base.py b/django/db/backends/mysql/base.py index cec3b04..fe2ebab 100644
a b def adapt_datetime_with_timezone_support(value, conv): 64 64 # Equivalent to DateTimeField.get_db_prep_value. Used only by raw SQL. 65 65 if settings.USE_TZ: 66 66 if timezone.is_naive(value): 67 warnings.warn(" SQLitereceived a naive datetime (%s)"67 warnings.warn("MySQL received a naive datetime (%s)" 68 68 " while time zone support is active." % value, 69 69 RuntimeWarning) 70 70 default_timezone = timezone.get_default_timezone() -
django/db/backends/mysql/creation.py
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)', -
django/db/backends/oracle/creation.py
diff --git a/django/db/backends/oracle/creation.py b/django/db/backends/oracle/creation.py index d9bf3df..0059644 100644
a b class DatabaseCreation(BaseDatabaseCreation): 16 16 # output (the "qn_" prefix is stripped before the lookup is performed. 17 17 18 18 data_types = { 19 'BigAutoField': 'NUMBER(19)', 19 20 'AutoField': 'NUMBER(11)', 20 21 'BooleanField': 'NUMBER(1) CHECK (%(qn_column)s IN (0,1))', 21 22 'CharField': 'NVARCHAR2(%(max_length)s)', -
django/db/backends/postgresql_psycopg2/creation.py
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)', -
django/db/backends/sqlite3/base.py
diff --git a/django/db/backends/sqlite3/base.py b/django/db/backends/sqlite3/base.py index 31a16b6..83aedbe 100644
a b class DatabaseOperations(BaseDatabaseOperations): 210 210 internal_type = field.get_internal_type() 211 211 if internal_type == 'DecimalField': 212 212 return util.typecast_decimal(field.format_number(value)) 213 elif internal_type and internal_type.endswith('IntegerField') or internal_type == 'AutoField': 213 elif (internal_type and internal_type.endswith('IntegerField') 214 or internal_type.endswith('AutoField')): 214 215 return int(value) 215 216 elif internal_type == 'DateField': 216 217 return parse_date(value) -
django/db/backends/sqlite3/creation.py
diff --git a/django/db/backends/sqlite3/creation.py b/django/db/backends/sqlite3/creation.py index c022b56..8a9952c 100644
a b class DatabaseCreation(BaseDatabaseCreation): 8 8 # thing" given more verbose field definitions, so leave them as is so that 9 9 # schema inspection is more useful. 10 10 data_types = { 11 'BigAutoField': 'integer', 11 12 'AutoField': 'integer', 12 13 'BooleanField': 'bool', 13 14 'CharField': 'varchar(%(max_length)s)', -
django/db/models/fields/__init__.py
diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py index d07851b..2ce0f3c 100644
a b class Field(object): 212 212 self.run_validators(value) 213 213 return value 214 214 215 def _internal_to_db_type(self, internal_type, connection): 216 data = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_") 217 try: 218 return connection.creation.data_types[internal_type] % data 219 except KeyError: 220 return None 221 215 222 def db_type(self, connection): 216 223 """ 217 224 Returns the database column data type for this field, for the provided … … class Field(object): 232 239 # mapped to one of the built-in Django field types. In this case, you 233 240 # can implement db_type() instead of get_internal_type() to specify 234 241 # exactly which wacky database column type you want to use. 235 data = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_") 236 try: 237 return (connection.creation.data_types[self.get_internal_type()] 238 % data) 239 except KeyError: 240 return None 242 return self._internal_to_db_type(self.get_internal_type(), connection) 243 244 def rel_db_type(self, connection): 245 """ 246 Returns the database column data type for related field referencing 247 to this. 248 """ 249 return self.db_type(connection) 241 250 242 251 @property 243 252 def unique(self): … … class AutoField(Field): 521 530 'invalid': _("'%s' value must be an integer."), 522 531 } 523 532 524 def __init__(self, *args, **kwargs):533 def __init__(self, verbose_name=None, name=None, big=False, **kwargs): 525 534 assert kwargs.get('primary_key', False) is True, \ 526 535 "%ss must have primary_key=True." % self.__class__.__name__ 527 536 kwargs['blank'] = True 528 Field.__init__(self, *args, **kwargs) 537 self.big = big 538 Field.__init__(self, verbose_name, name, **kwargs) 529 539 530 540 def get_internal_type(self): 531 return "AutoField" 541 return 'AutoField' if not self.big else 'BigAutoField' 542 543 def rel_db_type(self, connection): 544 db_type = 'IntegerField' if not self.big else 'BigIntegerField' 545 return self._internal_to_db_type(db_type, connection) 532 546 533 547 def to_python(self, value): 534 548 if value is None: … … class PositiveIntegerField(IntegerField): 1142 1156 def get_internal_type(self): 1143 1157 return "PositiveIntegerField" 1144 1158 1159 def rel_db_type(self, connection): 1160 if connection.features.related_fields_match_type: 1161 return self.db_type(connection) 1162 return self._internal_to_db_type('IntegerField', connection) 1163 1145 1164 def formfield(self, **kwargs): 1146 1165 defaults = {'min_value': 0} 1147 1166 defaults.update(kwargs) … … class PositiveSmallIntegerField(IntegerField): 1153 1172 def get_internal_type(self): 1154 1173 return "PositiveSmallIntegerField" 1155 1174 1175 def rel_db_type(self, connection): 1176 if connection.features.related_fields_match_type: 1177 return self.db_type(connection) 1178 return self._internal_to_db_type('IntegerField', connection) 1179 1156 1180 def formfield(self, **kwargs): 1157 1181 defaults = {'min_value': 0} 1158 1182 defaults.update(kwargs) -
django/db/models/fields/related.py
diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py index 08cc0a7..aa721ec 100644
a b class ForeignKey(RelatedField, Field): 1043 1043 # If the database needs similar types for key fields however, the only 1044 1044 # thing we can do is making AutoField an IntegerField. 1045 1045 rel_field = self.rel.get_related_field() 1046 if (isinstance(rel_field, AutoField) or 1047 (not connection.features.related_fields_match_type and 1048 isinstance(rel_field, (PositiveIntegerField, 1049 PositiveSmallIntegerField)))): 1050 return IntegerField().db_type(connection=connection) 1051 return rel_field.db_type(connection=connection) 1046 1047 return rel_field.rel_db_type(connection=connection) 1048 1052 1049 1053 1050 class OneToOneField(ForeignKey): 1054 1051 """ -
docs/ref/models/fields.txt
diff --git a/docs/ref/models/fields.txt b/docs/ref/models/fields.txt index 275c696..44b4eb4 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 -
new file tests/modeltests/bigautofield/models.py
diff --git a/tests/modeltests/bigautofield/__init__.py b/tests/modeltests/bigautofield/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/modeltests/bigautofield/models.py b/tests/modeltests/bigautofield/models.py new file mode 100644 index 0000000..95cf8c9
- + 1 # -*- coding: utf-8 -*- 2 from django.db import models 3 from django.utils.encoding import python_2_unicode_compatible 4 5 6 @python_2_unicode_compatible 7 class Village(models.Model): 8 name = models.CharField(max_length=100) 9 10 11 @python_2_unicode_compatible 12 class Population(models.Model): 13 village = models.ForeignKey(Village) 14 total = models.IntegerField() 15 16 17 @python_2_unicode_compatible 18 class City(models.Model): 19 id = models.AutoField(primary_key=True, big=True) 20 name = models.CharField(max_length=100) 21 22 23 @python_2_unicode_compatible 24 class Weather(models.Model): 25 city = models.ForeignKey(City) 26 temp = models.IntegerField() -
new file tests/modeltests/bigautofield/tests.py
diff --git a/tests/modeltests/bigautofield/tests.py b/tests/modeltests/bigautofield/tests.py new file mode 100644 index 0000000..2aa2f4b
- + 1 from __future__ import absolute_import, unicode_literals 2 3 from django.db import connection 4 from django.test import TestCase, skipUnlessDBFeature 5 6 from .models import Village, Population, City, Weather 7 8 9 class BigAutoFieldTest(TestCase): 10 11 def test_type(self): 12 field = Village._meta.get_field_by_name('id')[0] 13 self.assertEqual(field.get_internal_type(), 'AutoField') 14 types = { 15 'mysql': 'integer AUTO_INCREMENT', 'oracle': 'NUMBER(11)', 16 'postgresql': 'serial', 'sqlite': 'integer', 17 } 18 self.assertEqual(field.db_type(connection), types[connection.vendor]) 19 20 field = Population._meta.get_field_by_name('village')[0] 21 types = { 22 'mysql': 'integer', 'oracle': 'NUMBER(11)', 23 'postgresql': 'integer', 'sqlite': 'integer', 24 } 25 self.assertEqual(field.db_type(connection), types[connection.vendor]) 26 27 def test_big_type(self): 28 29 field = City._meta.get_field_by_name('id')[0] 30 self.assertEqual(field.get_internal_type(), 'BigAutoField') 31 types = { 32 'mysql': 'bigint AUTO_INCREMENT', 'oracle': 'NUMBER(19)', 33 'postgresql': 'bigserial', 'sqlite': 'integer', 34 } 35 self.assertEqual(field.db_type(connection), types[connection.vendor]) 36 37 field = Weather._meta.get_field_by_name('city')[0] 38 types = { 39 'mysql': 'bigint', 'oracle': 'NUMBER(19)', 40 'postgresql': 'bigint', 'sqlite': 'bigint', 41 } 42 self.assertEqual(field.db_type(connection), types[connection.vendor]) 43 44 def test_usage(self): 45 city_id = 8223372036854775807 46 c = City.objects.create(id=city_id, name='Moscow') 47 Weather.objects.create(city=c, temp=17) 48 49 c = City.objects.get(id=city_id) 50 w = Weather.objects.get(city_id=city_id) 51 self.assertEqual(c.id, city_id) 52 self.assertEqual(w.city_id, city_id) 53 54 c.delete() 55 56 with self.assertRaises(Weather.DoesNotExist): 57 Weather.objects.get(city_id=city_id)