1

我正在使用一个使用 MySQL big int 的遗留数据库,所以我设置了一个简单的自定义模型字段来处理这个问题:

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.

这适用于 django south 的 id/pk 字段 (mysql desc "| id | bigint(20) | NO | PRI | NULL | auto_increment |") 但其他模型中的 ForeignKey 字段引用字段创建为 int(11 ) 而不是 bigint(20)。

我假设我必须向 BigAutoField 添加一个自省规则,但文档中似乎没有提到这种规则(http://south.aeracode.org/docs/customfields.html)。

更新:目前使用 Django 1.1.1 和 South 0.6.2

更新 2: 似乎 Django 代码负责。

来自 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()

当我重载 AutoField 时,isinstance 返回 True,它默认为 IntegerField。猜猜我将不得不复制 AutoField 代码并这样做。. .

4

1 回答 1

1

当它显然是 FK 的问题时,你为什么要复制 AutoField?子类 ForeignKey 并返回正确的类型。

于 2010-03-22T10:08:04.747 回答