Ticket #17653: ticket_17653.diff

File ticket_17653.diff, 3.0 KB (added by Anssi Kääriäinen, 12 years ago)

Some initial work

  • django/db/backends/__init__.py

    diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py
    index ebe8875..5ee04cd 100644
    a b class BaseDatabaseOperations(object):  
    640640        """
    641641        return 'DEFAULT'
    642642
     643    def validate_autopk_value(self, value):
     644        """
     645        Certain backends have limitations for what values can be used as
     646        autofield values. This method will return True if the value is usable,
     647        else False.
     648        """
     649        return True
     650
    643651    def process_clob(self, value):
    644652        """
    645653        Returns the value of a CLOB column, for backends that return a locator
  • django/db/backends/mysql/base.py

    diff --git a/django/db/backends/mysql/base.py b/django/db/backends/mysql/base.py
    index 830808b..32a18c0 100644
    a b class DatabaseOperations(BaseDatabaseOperations):  
    226226        # 2**64 - 1, as recommended by the MySQL documentation
    227227        return 18446744073709551615L
    228228
     229    def validate_autopk_value(self, value):
     230        if value == 0:
     231            return False
     232        return True
     233
    229234    def quote_name(self, name):
    230235        if name.startswith("`") and name.endswith("`"):
    231236            return name # Quoting once is enough.
  • django/db/models/fields/__init__.py

    diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
    index 42b727d..6cc7ed1 100644
    a b class AutoField(Field):  
    535535            return None
    536536        return int(value)
    537537
     538    def get_db_prep_save(self, value, connection):
     539        prepared = self.get_prep_value(value)
     540        if not connection.ops.validate_autopk_value(prepared):
     541            raise ValueError('The database backend does not accept %s as a '
     542                             'value for field %s' % (prepared, self.name))
     543        return prepared
     544
    538545    def contribute_to_class(self, cls, name):
    539546        assert not cls._meta.has_auto_field, \
    540547               "A model can't have more than one AutoField."
  • tests/regressiontests/backends/tests.py

    diff --git a/tests/regressiontests/backends/tests.py b/tests/regressiontests/backends/tests.py
    index cfb662e..fdd5b83 100644
    a b class BackendLoadingTests(TestCase):  
    618618        self.assertRaisesRegexp(ImproperlyConfigured,
    619619            "Try using django.db.backends.sqlite3 instead",
    620620            load_backend, 'sqlite3')
     621
     622class MySQLAutoValueTests(TestCase):
     623    """
     624    Test a bug in MySQL where zero as a value to autofield gets ignored.
     625
     626    As this test is safe to run on other backends, no need to skip them.
     627    """
     628    def test_zero_as_autoval(self):
     629        models.Square.objects.create(id=0, root=0, square=1)
     630        models.Square.objects.all().delete()
     631        s = models.Square(id=0, root=0, square=1)
     632        models.Square.objects.bulk_create([s])
     633        models.Square.objects.all().delete()
     634        s = models.Square(id=0, root=0, square=1)
     635        s.save()
Back to Top