开发者

Django South Foreign Keys referring to pks with Custom Fields

开发者 https://www.devze.com 2022-12-23 12:30 出处:网络
I\'m working with a legacy database which uses the MySQL big int so I setup a simple custom model field to handle this:

I'm working with a legacy database which uses the MySQL big int so I setup a simple custom model field to handle this:

class BigAutoField(models.AutoField):
    def get_internal_type(self):
        return "BigAutoField"

    def db_type(self):
        return 'bigint AUTO_INCREMENT' # Note this won't work with Oracle.

This works fine w开发者_如何学Cith django south for the id/pk fields (mysql desc "| id | bigint(20) | NO | PRI | NULL | auto_increment |") but the ForeignKey fields in other models the referring fields are created as int(11) rather than bigint(20).

I assume I have to add an introspection rule to the BigAutoField but there doesn't seem to be a mention of this sort of rule in the documentation (http://south.aeracode.org/docs/customfields.html).

Update: Currently using Django 1.1.1 and South 0.6.2

Update 2: Seems the Django code is responsible.

from django.db.models.fields.related.ForeignKey.db_type():

rel_field = self.rel.get_related_field()
if (isinstance(rel_field, AutoField) or
        (not connection.features.related_fields_match_type and
         isinstance(rel_field, (PositiveIntegerField,
                                PositiveSmallIntegerField)))):
    return IntegerField().db_type()

As I am overloading AutoField isinstance is returning True and it is defaulting to IntegerField. Guess I will have to copy the AutoField code and do it that way . . .


Why would you want to copy AutoField when it's clearly FK's problem? Subclass ForeignKey and return correct type instead.

0

精彩评论

暂无评论...
验证码 换一张
取 消