我在开发一个Django应用来从Bugzilla数据库中拉取数据,在获取Bug标记信息时遇到了问题。
模型定义:
class Bugzilla_bugs(models.Model):
class Meta:
db_table = 'bugs'
bug_id = models.IntegerField(primary_key=True)
...
class Bugzilla_flagtypes(models.Model):
class Meta:
db_table = 'flagtypes'
name = models.CharField(max_length=50,unique=True)
description = models.TextField()
...
class Bugzilla_flags(models.Model):
class Meta:
db_table = 'flags'
type_id = models.ForeignKey(Bugzilla_flagtypes,related_name='flagtype',db_column='type_id',to_field='name')
status = models.CharField(max_length=50)
bug_id = models.ForeignKey(Bugzilla_bugs,related_name='flaglines',db_column='bug_id',to_field='bug_id')
当我想获取特定Bug的标记线时,会出现异常:
bug = Bugzilla_bugs.objects.using('bugzilla').get(bug_id=12345)
bug.flaglines.get(type_id="UnlocksBranch")
# 输出:
>>> bug.flaglines.get(type_id__name="UnlocksBranch")
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py", line 92, in manager_method
return getattr(self.get_queryset(), name)(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 351, in get
num = len(clone)
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 122, in __len__
self._fetch_all()
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 966, in _fetch_all
self._result_cache = list(self.iterator())
File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 265, in iterator
for row in compiler.results_iter():
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 700, in results_iter
for rows in self.execute_sql(MULTI):
File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 786, in execute_sql
cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 81, in execute
return super(CursorDebugWrapper, self).execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 65, in execute
return self.cursor.execute(sql, params)
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/base.py", line 128, in execute
return self.cursor.execute(query, args)
File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 176, in execute
if not self._defer_warnings: self._warning_check()
File "/usr/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 92, in _warning_check
warn(w[-1], self.Warning, 3)
Warning: Incorrect integer value: 'UnlocksBranch' for column 'type_id' at row 1
解决方案
问题的原因在于Bugzilla_flags模型中的type_id字段与数据库中的type_id列不匹配。
在数据库中,type_id列存储的是整型主键,但是在模型中,type_id字段却被定义为外键,并且指定了to_field='name', 这就导致了Django在检索数据时无法正确匹配。
要解决这个问题,需要将Bugzilla_flags模型中的type_id字段定义为整型字段,并移除to_field参数。
修改后的模型定义如下:
class Bugzilla_flags(models.Model):
class Meta:
db_table = 'flags'
type_id = models.IntegerField(db_column='type_id')
status = models.CharField(max_length=50)
bug_id = models.ForeignKey(Bugzilla_bugs,related_name='flaglines',db_column='bug_id',to_field='bug_id')
修改后,就可以正确检索数据了。