Ticket #56: 56_bigint.diff

File 56_bigint.diff, 9.5 KB (added by pzinovkin, 3 years ago)

Alternative approach to fix #56

  • django/db/backends/__init__.py

    diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py
    index 2762350..2c56ef6 100644
    a b class BaseDatabaseOperations(object): 
    847847        internal_type = field.get_internal_type()
    848848        if internal_type == 'DecimalField':
    849849            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')):
    851852            return int(value)
    852853        elif internal_type in ('DateField', 'DateTimeField', 'TimeField'):
    853854            return value
  • 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): 
    66    # be interpolated against the values of Field.__dict__ before being output.
    77    # If a column type is set to None, it won't be included in the output.
    88    data_types = {
     9        'BigAutoField':      'bigint AUTO_INCREMENT',
    910        'AutoField':         'integer AUTO_INCREMENT',
    1011        'BooleanField':      'bool',
    1112        '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 2f096f7..7e97271 100644
    a b class DatabaseCreation(BaseDatabaseCreation): 
    1515    # output (the "qn_" prefix is stripped before the lookup is performed.
    1616
    1717    data_types = {
     18        'BigAutoField':                 'NUMBER(11)',
    1819        'AutoField':                    'NUMBER(11)',
    1920        'BooleanField':                 'NUMBER(1) CHECK (%(qn_column)s IN (0,1))',
    2021        '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): 
    1010    # be interpolated against the values of Field.__dict__ before being output.
    1111    # If a column type is set to None, it won't be included in the output.
    1212    data_types = {
     13        'BigAutoField':      'bigserial',
    1314        'AutoField':         'serial',
    1415        'BooleanField':      'boolean',
    1516        '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 7ce9dd3..5a26344 100644
    a b class DatabaseOperations(BaseDatabaseOperations): 
    191191        internal_type = field.get_internal_type()
    192192        if internal_type == 'DecimalField':
    193193            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')):
    195196            return int(value)
    196197        elif internal_type == 'DateField':
    197198            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 efdc457..51ceca9 100644
    a b class DatabaseCreation(BaseDatabaseCreation): 
    77    # thing" given more verbose field definitions, so leave them as is so that
    88    # schema inspection is more useful.
    99    data_types = {
     10        'BigAutoField':                 'integer',
    1011        'AutoField':                    'integer',
    1112        'BooleanField':                 'bool',
    1213        '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 f694958..668f367 100644
    a b class Field(object): 
    205205        self.run_validators(value)
    206206        return value
    207207
     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
    208215    def db_type(self, connection):
    209216        """
    210217        Returns the database column data type for this field, for the provided
    class Field(object): 
    225232        # mapped to one of the built-in Django field types. In this case, you
    226233        # can implement db_type() instead of get_internal_type() to specify
    227234        # 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)
    234243
    235244    @property
    236245    def unique(self):
    class AutoField(Field): 
    514523        'invalid': _(u"'%s' value must be an integer."),
    515524    }
    516525
    517     def __init__(self, *args, **kwargs):
     526    def __init__(self, verbose_name=None, name=None, big=False, **kwargs):
    518527        assert kwargs.get('primary_key', False) is True, \
    519528               "%ss must have primary_key=True." % self.__class__.__name__
    520529        kwargs['blank'] = True
    521         Field.__init__(self, *args, **kwargs)
     530        self.big = big
     531        Field.__init__(self, verbose_name, name, **kwargs)
    522532
    523533    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)
    525539
    526540    def to_python(self, value):
    527541        if value is None:
    class PositiveIntegerField(IntegerField): 
    11391153    def get_internal_type(self):
    11401154        return "PositiveIntegerField"
    11411155
     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
    11421161    def formfield(self, **kwargs):
    11431162        defaults = {'min_value': 0}
    11441163        defaults.update(kwargs)
    class PositiveSmallIntegerField(IntegerField): 
    11501169    def get_internal_type(self):
    11511170        return "PositiveSmallIntegerField"
    11521171
     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
    11531177    def formfield(self, **kwargs):
    11541178        defaults = {'min_value': 0}
    11551179        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 a16f955..0783590 100644
    a b class ForeignKey(RelatedField, Field): 
    10181018        return super(ForeignKey, self).formfield(**defaults)
    10191019
    10201020    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.
    10271021        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)
    10341023
    10351024class OneToOneField(ForeignKey):
    10361025    """
  • docs/ref/models/fields.txt

    diff --git a/docs/ref/models/fields.txt b/docs/ref/models/fields.txt
    index eab7ad9..d88368a 100644
    a b Field types 
    320320``AutoField``
    321321-------------
    322322
    323 .. class:: AutoField(**options)
     323.. class:: AutoField([big=False, **options])
    324324
    325325An :class:`IntegerField` that automatically increments
    326326according to available IDs. You usually won't need to use this directly; a
    327327primary key field will automatically be added to your model if you don't specify
    328328otherwise. See :ref:`automatic-primary-key-fields`.
    329329
     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
    330339``BigIntegerField``
    331340-------------------
    332341
Back to Top