Django South 外键引用带有自定义字段的 pks
我正在使用一个使用 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) | mysql desc "| id | bigint(20) | NO | PRI | NULL | auto_increment |"),但其他模型中的外键字段被创建为 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.lated.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 代码并这样做。 。 。
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 with 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 . . .
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当这显然是 FK 的问题时,为什么要复制 AutoField?子类化ForeignKey并返回正确的类型。
Why would you want to copy AutoField when it's clearly FK's problem? Subclass ForeignKey and return correct type instead.