【问题标题】:Django South Foreign Keys referring to pks with Custom FieldsDjango South外键引用带有自定义字段的pks
【发布时间】:2011-01-30 04:03:40
【问题描述】:

我正在使用一个使用 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 代码并这样做。 . .

【问题讨论】:

    标签: django django-south


    【解决方案1】:

    既然是 FK 的问题,为什么还要复制 AutoField?子类 ForeignKey 并返回正确的类型。

    【讨论】:

    • 是的,我在替换所有 AutoField 代码后发现了这一点。 ForeignKey 然后将自己设置为 bigint AUTO_INCREMENT,这会严重破坏事情。 Django 1.2 提供了 bigint,所以我将在我的南迁移中使用手动迁移 sql 查询来解决这个问题,直到我们升级到 1.2 和 0.7。
    猜你喜欢
    • 1970-01-01
    • 2016-02-28
    • 2015-06-13
    • 1970-01-01
    • 2011-03-11
    • 2011-10-28
    • 2012-10-05
    • 2021-12-09
    • 2013-11-14
    相关资源
    最近更新 更多