Ticket #56: 56_autoincrement_unsigned.diff

File 56_autoincrement_unsigned.diff, 8.7 KB (added by pzinovkin, 13 years ago)
  • django/db/backends/__init__.py

    diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py
    index 7674f5c..1e9cca2 100644
    a b class BaseDatabaseOperations(object):  
    831831        internal_type = field.get_internal_type()
    832832        if internal_type == 'DecimalField':
    833833            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')):
    835836            return int(value)
    836837        elif internal_type in ('DateField', 'DateTimeField', 'TimeField'):
    837838            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..86164ce 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        'UnsignedAutoField': 'integer unsigned 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 44f8d91..dc38f4e 100644
    a b class DatabaseCreation(BaseDatabaseCreation):  
    1515    # output (the "qn_" prefix is stripped before the lookup is performed.
    1616
    1717    data_types = {
     18        'UnsignedAutoField':            '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..30aef55 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        'UnsignedAutoField': '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 f5f0c64..3a97408 100644
    a b class DatabaseOperations(BaseDatabaseOperations):  
    178178        internal_type = field.get_internal_type()
    179179        if internal_type == 'DecimalField':
    180180            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')):
    182183            return int(value)
    183184        elif internal_type == 'DateField':
    184185            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 cd39356..2e9429c 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        'UnsignedAutoField':            '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 22546c2..aee84cb 100644
    a b class Field(object):  
    201201        self.run_validators(value)
    202202        return value
    203203
     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
    204211    def db_type(self, connection):
    205212        """
    206213        Returns the database column data type for this field, for the provided
    class Field(object):  
    221228        # mapped to one of the built-in Django field types. In this case, you
    222229        # can implement db_type() instead of get_internal_type() to specify
    223230        # 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)
    230239
    231240    @property
    232241    def unique(self):
    class AutoField(Field):  
    510519        'invalid': _(u"'%s' value must be an integer."),
    511520    }
    512521
    513     def __init__(self, *args, **kwargs):
     522    def __init__(self, verbose_name=None, name=None, unsigned=False, **kwargs):
    514523        assert kwargs.get('primary_key', False) is True, \
    515524               "%ss must have primary_key=True." % self.__class__.__name__
    516525        kwargs['blank'] = True
    517         Field.__init__(self, *args, **kwargs)
     526        self.unsigned = unsigned
     527        Field.__init__(self, verbose_name, name, **kwargs)
    518528
    519529    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)
    521536
    522537    def to_python(self, value):
    523538        if value is None:
    class PositiveIntegerField(IntegerField):  
    11181133    def get_internal_type(self):
    11191134        return "PositiveIntegerField"
    11201135
     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
    11211141    def formfield(self, **kwargs):
    11221142        defaults = {'min_value': 0}
    11231143        defaults.update(kwargs)
    class PositiveSmallIntegerField(IntegerField):  
    11291149    def get_internal_type(self):
    11301150        return "PositiveSmallIntegerField"
    11311151
     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
    11321157    def formfield(self, **kwargs):
    11331158        defaults = {'min_value': 0}
    11341159        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 53f9b9f..7dcdb7d 100644
    a b class ForeignKey(RelatedField, Field):  
    10081008        return super(ForeignKey, self).formfield(**defaults)
    10091009
    10101010    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.
    10171011        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)
    10241013
    10251014class OneToOneField(ForeignKey):
    10261015    """
Back to Top