0

当我使用 对 ForeignKey 字段进行过滤时__isnull=True,会引发此异常:

DatabaseError: This query is not supported by the database.

但是,__isnull=False只要没有其他不等式过滤器(我期望),就可以在 ForeignKey 上工作。并__isnull=True适用于其他字段类型。

那么为什么__isnull=True不能在 ForeignKey 上工作呢?似乎DBIndexer试图使其工作,如下所示: https ://github.com/django-nonrel/django-dbindexer/blob/dbindexer-1.4/dbindexer/backends.py

但是djangotoolbox中有一个异常:

  File "/Users//Documents/workspace/-gae-dev/src/django/db/models/query.py", line 107, in _result_iter
    self._fill_cache()
  File "/Users//Documents/workspace/-gae-dev/src/django/db/models/query.py", line 774, in _fill_cache
    self._result_cache.append(self._iter.next())
  File "/Users//Documents/workspace/-gae-dev/src/django/db/models/query.py", line 275, in iterator
    for row in compiler.results_iter():
  File "/Users//Documents/workspace/-gae-dev/src/djangotoolbox/db/basecompiler.py", line 337, in results_iter
    results = self.build_query(fields).fetch(
  File "/Users//Documents/workspace/-gae-dev/src/djangotoolbox/db/basecompiler.py", line 428, in build_query
    self.check_query()
  File "/Users//Documents/workspace/-gae-dev/src/djangotoolbox/db/basecompiler.py", line 409, in check_query
    raise DatabaseError("This query is not supported by the database.")

我确实在 djangoappengine 中遇到了以下注释掉的测试用例,我想知道是否指的是同一个问题?

    def test_is_null(self):
        self.assertEquals(FieldsWithOptionsModel.objects.filter(
            floating_point__isnull=True).count(), 0)

        FieldsWithOptionsModel(
            integer=5.4, email='shinra.tensai@sixpaths.com',
            time=datetime.datetime.now().time()).save()

        self.assertEquals(FieldsWithOptionsModel.objects.filter(
            floating_point__isnull=True).count(), 1)

        # XXX: These filters will not work because of a Django bug.
#        self.assertEquals(FieldsWithOptionsModel.objects.filter(
#            foreign_key=None).count(), 1)

        # (it uses left outer joins if checked against isnull)
#        self.assertEquals(FieldsWithOptionsModel.objects.filter(
#            foreign_key__isnull=True).count(), 1)
4

1 回答 1

2

NonRel 项目中的 Alex Burgel 让我明白了:

NonRel/dbindexer 项目修复了这个查询(否则由于这个 Django 错误而无法工作:https ://code.djangoproject.com/ticket/10790 )。要设置 dbindexer:

  • 当然,将其添加到 INSTALLED_APPS
  • 同样在 settings.py 中,设置 DATABASES['default]['ENGINE']='dbindexer'
  • 同样在 settings.py 中,设置 DBINDEXER_BACKENDS 以使用 FKNullFix。例如:

DBINDEXER_BACKENDS = ('dbindexer.backends.BaseResolver', 'dbindexer.backends.FKNullFix', 'dbindexer.backends.InMemoryJOINResolver', 'dbindexer.backends.ConstantFieldJOINResolver', )

于 2013-07-15T13:10:19.020 回答