diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py
index ebe8875..5ee04cd 100644
a
|
b
|
class BaseDatabaseOperations(object):
|
640 | 640 | """ |
641 | 641 | return 'DEFAULT' |
642 | 642 | |
| 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 | |
643 | 651 | def process_clob(self, value): |
644 | 652 | """ |
645 | 653 | Returns the value of a CLOB column, for backends that return a locator |
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):
|
226 | 226 | # 2**64 - 1, as recommended by the MySQL documentation |
227 | 227 | return 18446744073709551615L |
228 | 228 | |
| 229 | def validate_autopk_value(self, value): |
| 230 | if value == 0: |
| 231 | return False |
| 232 | return True |
| 233 | |
229 | 234 | def quote_name(self, name): |
230 | 235 | if name.startswith("`") and name.endswith("`"): |
231 | 236 | return name # Quoting once is enough. |
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):
|
535 | 535 | return None |
536 | 536 | return int(value) |
537 | 537 | |
| 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 | |
538 | 545 | def contribute_to_class(self, cls, name): |
539 | 546 | assert not cls._meta.has_auto_field, \ |
540 | 547 | "A model can't have more than one AutoField." |
diff --git a/tests/regressiontests/backends/tests.py b/tests/regressiontests/backends/tests.py
index cfb662e..fdd5b83 100644
a
|
b
|
class BackendLoadingTests(TestCase):
|
618 | 618 | self.assertRaisesRegexp(ImproperlyConfigured, |
619 | 619 | "Try using django.db.backends.sqlite3 instead", |
620 | 620 | load_backend, 'sqlite3') |
| 621 | |
| 622 | class 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() |