diff --git a/django/db/backends/__init__.py b/django/db/backends/__init__.py
index 4487bde..01144eb 100644
a
|
b
|
class BaseDatabaseOperations(object):
|
671 | 671 | # need not necessarily be implemented using "LIKE" in the backend. |
672 | 672 | prep_for_iexact_query = prep_for_like_query |
673 | 673 | |
| 674 | def value_to_db_auto(self, value): |
| 675 | """ |
| 676 | Transform a value to an object compatible with the auto field required |
| 677 | by the backend driver for auto columns. |
| 678 | """ |
| 679 | if value is None: |
| 680 | return None |
| 681 | return int(value) |
| 682 | |
674 | 683 | def value_to_db_date(self, value): |
675 | 684 | """ |
676 | 685 | Transform a date value to an object compatible with what is expected |
diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
index d79c172..42cc98a 100644
a
|
b
|
class AutoField(Field):
|
474 | 474 | return db_type |
475 | 475 | |
476 | 476 | def to_python(self, value): |
477 | | if value is None: |
478 | | return value |
479 | | try: |
480 | | return int(value) |
481 | | except (TypeError, ValueError): |
| 477 | if not (value is None or isinstance(value, (basestring, int, long))): |
482 | 478 | raise exceptions.ValidationError(self.error_messages['invalid']) |
| 479 | return value |
483 | 480 | |
484 | 481 | def validate(self, value, model_instance): |
485 | 482 | pass |
486 | 483 | |
487 | 484 | def get_prep_value(self, value): |
488 | | if value is None: |
489 | | return None |
490 | | return int(value) |
| 485 | return value |
| 486 | |
| 487 | def get_db_prep_value(self, value, connection, prepared=False): |
| 488 | # Casts AutoField into the format expected by the backend |
| 489 | if not prepared: |
| 490 | value = self.get_prep_value(value) |
| 491 | return connection.ops.value_to_db_auto(value) |
491 | 492 | |
492 | 493 | def contribute_to_class(self, cls, name): |
493 | 494 | assert not cls._meta.has_auto_field, "A model can't have more than one AutoField." |
diff --git a/django/db/models/fields/related.py b/django/db/models/fields/related.py
index 1318706..7dceb34 100644
a
|
b
|
|
1 | 1 | from django.conf import settings |
2 | | from django.db import connection, router, transaction |
| 2 | from django.db import connection, router, transaction, connections |
3 | 3 | from django.db.backends import util |
4 | 4 | from django.db.models import signals, get_model |
5 | 5 | from django.db.models.fields import (AutoField, Field, IntegerField, |
… |
… |
class ForeignKey(RelatedField, Field):
|
873 | 873 | return None |
874 | 874 | else: |
875 | 875 | return self.rel.get_related_field().get_db_prep_save(value, |
876 | | connection=connection) |
| 876 | connection=connections[router.db_for_read(self.rel.to)]) |
877 | 877 | |
878 | 878 | def value_to_string(self, obj): |
879 | 879 | if not obj: |
… |
… |
class ForeignKey(RelatedField, Field):
|
924 | 924 | # If the database needs similar types for key fields however, the only |
925 | 925 | # thing we can do is making AutoField an IntegerField. |
926 | 926 | rel_field = self.rel.get_related_field() |
927 | | if (isinstance(rel_field, AutoField) or |
928 | | (not connection.features.related_fields_match_type and |
929 | | isinstance(rel_field, (PositiveIntegerField, |
930 | | PositiveSmallIntegerField)))): |
931 | | return IntegerField().db_type(connection=connection) |
932 | | return rel_field.db_type(connection=connection) |
| 927 | return rel_field.related_db_type(connection=connections[router.db_for_read(rel_field.model)]) |
933 | 928 | |
934 | 929 | class OneToOneField(ForeignKey): |
935 | 930 | """ |