2

我遇到了非常奇怪的prefetch_related呼叫行为。这是插图:

# First define two sketch models, just for convenience of the further talk.

class Secondary(models.Model):
    pass

class Primary(models.Model):
    secondaries = models.ManyToManyField(Secondary)

# Just to make clear, EVERY Primary object in my system has at least one
# related Secondary object.

# Now prepare a query.

primaries = Primary.objects.filter(...)\
                           .order_by(...)\
                           .prefetch_related('secondary')

# Iterating:

for primary in primaries:
    if not primary.secondaries.all():
        # So we have found an object that is said to not have
        # any relatives.  Re-query this particular object.
        # This part is hit in my code, although it should not.
        primary = Primary.objects.get(pk=primary.pk)
    for secondary in primary.secondaries.all():
        # Voila, there are relatives!
        # This part was not hit for some objects until I added
        # the re-query part above.
        pass

为了清楚Primary起见,我的系统中没有没有相关Secondary对象的对象,但上面的代码仍然针对其中一些(总是相同的)命中了重新查询部分,并且重新查询获取了那些是失踪。更奇怪的是,我可以看到一些主节点在它们的辅助节点中出现重复secondaries.all()——总体印象是 ORM 错误地将一些辅助节点集合连接到错误的主节点。

有什么问题?这是 Django 或数据库的一些错误吗?

我使用 Django 1.10.5、psycopg2 2.7.3 和 Postgres 9.6。

更新:我发现问题更严重:有时 ORM 会返回不完整的相关对象列表,所以我上面解释的解决方法没有帮助。我们不得不删除 prefetch_related 调用,因为显然我们不能依赖它返回的数据。

更新 2:正如 Daniel 在评论中所问的,这里有一些真实的 SQL 查询(尽管不是来自我们遇到问题的系统)。backend_build是“主要”模型,并且有几个“次要”模型:backend_buildproblembackend_sanityproblembackend_runproblem— 我们使用 django_polymorphic 来表示它们,基本模型是backend_problem.

Python 代码如下所示:

builds = Build.objects.filter(
    branch__active=True,
    type__active=True,
    finish_timestamp__gt=timezone.now() - timedelta(days=10))\
 .order_by('-finish_timestamp')\
 .prefetch_related('problems')

for build in builds:
  for problem in build.problems.all():
    print problem.id  # just a stub code to use results of the query.

以下是生成的 SQL 查询:

SELECT "backend_build"."teamcity_id", "backend_build"."status", "backend_build"."finish_timestamp", "backend_build"."type_id", "backend_build"."branch_id", "backend_build"."revision"
  FROM "backend_build"
  INNER JOIN "backend_buildtype" ON ("backend_build"."type_id" = "backend_buildtype"."id")
  INNER JOIN "backend_branch" ON ("backend_build"."branch_id" = "backend_branch"."id")
  WHERE ("backend_build"."finish_timestamp" > \'2017-08-18T06:35:21.322000+00:00\'::timestamptz AND "backend_buildtype"."active" = true AND "backend_branch"."active" = true)
  ORDER BY "backend_build"."finish_timestamp" DESC 

SELECT "backend_problem"."id", "backend_problem"."polymorphic_ctype_id", "backend_problem"."generic_type", "backend_problem"."startrack_id", "backend_problem"."useful", "backend_problem"."status", "backend_problem"."summary"
  FROM "backend_problem"
  INNER JOIN "backend_build_problems" ON ("backend_problem"."id" = "backend_build_problems"."problem_id")
  WHERE "backend_build_problems"."build_id" = 18984809

SELECT "backend_problem"."id", "backend_problem"."polymorphic_ctype_id", "backend_problem"."generic_type", "backend_problem"."startrack_id", "backend_problem"."useful", "backend_problem"."status", "backend_problem"."summary", "backend_sanityproblem"."problem_ptr_id", "backend_sanityproblem"."code", "backend_sanityproblem"."latest_occurred"
  FROM "backend_sanityproblem"
  INNER JOIN "backend_problem" ON ("backend_sanityproblem"."problem_ptr_id" = "backend_problem"."id")
  WHERE "backend_sanityproblem"."problem_ptr_id" IN (9251, 9252, 9253, 9254, 9255, 9256, 9257, 9259, 9261, 9262, 9263, 9264, 9268, 9269, 9270, 9271, 9272, 9273, 9274, 9275, 9276, 9277, 9280, 9283, 9285, 9287, 9290, 9293, 9294, 9295, 9297, 9302, 9303, 9304, 9306, 9307, 9309, 9312, 9313, 9314, 9316, 9317, 9319, 9321, 9322, 9062, 9063, 9066, 9068, 9092, 9107, 9109, 9112, 9648, 9649, 9650, 9651, 9652, 9653, 9654, 9655, 9656, 9657, 9658, 9659, 9660, 9661, 9662, 9663, 9664, 9665, 9666, 9667, 9668, 9669, 9670, 9671, 9672, 9673, 9674, 9675, 9676, 9677, 9678, 9679, 9680, 9681, 9682, 9683, 9684, 9685, 9686, 9687, 9688, 9689, 9690, 9691, 9692, 9693, 9694)

SELECT "backend_problem"."id", "backend_problem"."polymorphic_ctype_id", "backend_problem"."generic_type", "backend_problem"."startrack_id", "backend_problem"."useful", "backend_problem"."status", "backend_problem"."summary", "backend_sanityproblem"."problem_ptr_id", "backend_sanityproblem"."code", "backend_sanityproblem"."latest_occurred"
  FROM "backend_sanityproblem"
  INNER JOIN "backend_problem" ON ("backend_sanityproblem"."problem_ptr_id" = "backend_problem"."id")
  WHERE "backend_sanityproblem"."problem_ptr_id" IN (9344, 9345, 9488, 9489, 9508, 9509, 9510, 9511, 9512, 9513, 9399, 9401, 9402, 9403, 9426, 9436, 9572, 9573, 9574, 9575, 9330, 9337, 9338, 9339, 9340, 9341, 9342)

SELECT "backend_problem"."id", "backend_problem"."polymorphic_ctype_id", "backend_problem"."generic_type", "backend_problem"."startrack_id", "backend_problem"."useful", "backend_problem"."status", "backend_problem"."summary"
  FROM "backend_problem"
  INNER JOIN "backend_build_problems" ON ("backend_problem"."id" = "backend_build_problems"."problem_id")
  WHERE "backend_build_problems"."build_id" = 18944441

SELECT "backend_problem"."id", "backend_problem"."polymorphic_ctype_id", "backend_problem"."generic_type", "backend_problem"."startrack_id", "backend_problem"."useful", "backend_problem"."status", "backend_problem"."summary", "backend_buildproblem"."problem_ptr_id", "backend_buildproblem"."stage"
  FROM "backend_buildproblem"
  INNER JOIN "backend_problem" ON ("backend_buildproblem"."problem_ptr_id" = "backend_problem"."id")
  WHERE "backend_buildproblem"."problem_ptr_id" IN (9600)

SELECT "backend_problem"."id", "backend_problem"."polymorphic_ctype_id", "backend_problem"."generic_type", "backend_problem"."startrack_id", "backend_problem"."useful", "backend_problem"."status", "backend_problem"."summary"
  FROM "backend_problem"
  INNER JOIN "backend_build_problems" ON ("backend_problem"."id" = "backend_build_problems"."problem_id")
  WHERE "backend_build_problems"."build_id" = 18944330

像这样的查询还有很多,这里就省略了。从上面看起来很清楚,系统 ritst 查询主模型,然后请求每个主对象的关系,并考虑它们的多态类型。

4

1 回答 1

1

我怀疑您的问题源于您有多个具有相同基本模型的辅助模型。可能有一个内部缓存会被每个查询覆盖。尝试将prefetch_related语句限制为problems模型:

.prefetch_related('problems')

或者也许它与 django-polymorphic 的这个问题有关?

于 2017-08-28T10:44:06.553 回答