Ticket #14856: related_db_type.diff

File related_db_type.diff, 5.6 KB (added by Waldemar Kornewald, 14 years ago)

patch against trunk

  • django/db/backends/creation.py

    diff -r 9b24e08becc2 django/db/backends/creation.py
    a b  
    33
    44from django.conf import settings
    55from django.core.management import call_command
     6from django.utils.datastructures import DictWrapper
    67
    78# The prefix to put on the default database name when creating
    89# the test database.
     
    2627        shorten identifying names.
    2728        """
    2829        return '%x' % (abs(hash(args)) % 4294967296L)  # 2**32
     30   
     31    def db_type(self, field):
     32        return self._db_type(field, field.get_internal_type())
     33
     34    def related_db_type(self, field):
     35        return self._db_type(field, field.get_related_internal_type())
     36
     37    def _db_type(self, field, internal_type):
     38        data = DictWrapper(field.__dict__, self.connection.ops.quote_name, "qn_")
     39        try:
     40            return self.connection.creation.data_types[internal_type] % data
     41        except KeyError:
     42            return None
    2943
    3044    def sql_create_model(self, model, style, known_models=set()):
    3145        """
  • django/db/models/fields/__init__.py

    diff -r 9b24e08becc2 django/db/models/fields/__init__.py
    a b  
    1313from django.conf import settings
    1414from django import forms
    1515from django.core import exceptions, validators
    16 from django.utils.datastructures import DictWrapper
    1716from django.utils.functional import curry
    1817from django.utils.text import capfirst
    1918from django.utils.translation import ugettext_lazy as _
     
    215214        # mapped to one of the built-in Django field types. In this case, you
    216215        # can implement db_type() instead of get_internal_type() to specify
    217216        # exactly which wacky database column type you want to use.
    218         data = DictWrapper(self.__dict__, connection.ops.quote_name, "qn_")
    219         try:
    220             return connection.creation.data_types[self.get_internal_type()] % data
    221         except KeyError:
    222             return None
     217        return connection.creation.db_type(self)
     218
     219    def related_db_type(self, connection):
     220        # This is the db_type used by a ForeignKey.
     221        return connection.creation.related_db_type(self)
    223222
    224223    def unique(self):
    225224        return self._unique or self.primary_key
     
    252251    def get_internal_type(self):
    253252        return self.__class__.__name__
    254253
     254    def get_related_internal_type(self):
     255        return self.get_internal_type()
     256
    255257    def pre_save(self, model_instance, add):
    256258        "Returns field's value just before saving."
    257259        return getattr(model_instance, self.attname)
     
    462464    def get_internal_type(self):
    463465        return "AutoField"
    464466
     467    def get_related_internal_type(self):
     468        return "RelatedAutoField"
     469
     470    def related_db_type(self, connection):
     471        db_type = super(AutoField, self).related_db_type(connection=connection)
     472        if db_type is None:
     473            return IntegerField().db_type(connection=connection)
     474        return db_type
     475
    465476    def to_python(self, value):
    466477        if value is None:
    467478            return value
     
    979990class PositiveIntegerField(IntegerField):
    980991    description = _("Integer")
    981992
     993    def related_db_type(self, connection):
     994        if not connection.features.related_fields_match_type:
     995            return IntegerField().related_db_type(connection=connection)
     996        return super(PositiveIntegerField, self).related_db_type(
     997            connection=connection)
     998
    982999    def get_internal_type(self):
    9831000        return "PositiveIntegerField"
    9841001
     
    9891006
    9901007class PositiveSmallIntegerField(IntegerField):
    9911008    description = _("Integer")
     1009
     1010    def related_db_type(self, connection):
     1011        if not connection.features.related_fields_match_type:
     1012            return IntegerField().related_db_type(connection=connection)
     1013        return super(PositiveSmallIntegerField, self).related_db_type(
     1014            connection=connection)
     1015
    9921016    def get_internal_type(self):
    9931017        return "PositiveSmallIntegerField"
    9941018
  • django/db/models/fields/related.py

    diff -r 9b24e08becc2 django/db/models/fields/related.py
    a b  
    11from django.conf import settings
    2 from django.db import connection, router, transaction
     2from django.db import connection, router, transaction, connections
    33from django.db.backends import util
    44from django.db.models import signals, get_model
    55from django.db.models.fields import (AutoField, Field, IntegerField,
     
    859859            return None
    860860        else:
    861861            return self.rel.get_related_field().get_db_prep_save(value,
    862                 connection=connection)
     862                    connection=connections[router.db_for_read(self.rel.to)])
    863863
    864864    def value_to_string(self, obj):
    865865        if not obj:
     
    908908        # If the database needs similar types for key fields however, the only
    909909        # thing we can do is making AutoField an IntegerField.
    910910        rel_field = self.rel.get_related_field()
    911         if (isinstance(rel_field, AutoField) or
    912                 (not connection.features.related_fields_match_type and
    913                 isinstance(rel_field, (PositiveIntegerField,
    914                                        PositiveSmallIntegerField)))):
    915             return IntegerField().db_type(connection=connection)
    916         return rel_field.db_type(connection=connection)
     911        return rel_field.related_db_type(connection=connections[router.db_for_read(rel_field.model)])
    917912
    918913class OneToOneField(ForeignKey):
    919914    """
Back to Top