tortoise / tortoise/tortoise-orm

Prefetch is not working as expected

Open
#671 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.6k
Forks
516
Avg merge
2d 21h
Merged PRs (30d)
9

Description

Describe the bug
I am trying to query a model Well with filter and offset/limit.
then for each Well I want to get the latest scenario preferably approved if there is one.
then for each scenario get its decline_parameters

    results = await models.Well.filter(field_name=field_name)\
        .order_by("well_name").offset(offset).limit(limit)\
        .prefetch_related(\
            Prefetch("scenarios",queryset=models.Scenarios.all()\
            .order_by("-is_approved","-created_at").limit(1).prefetch_related(\
                    Prefetch("decline_parameters",queryset=models.Decline_Parameters.filter(label="best").all(),\ 
                    to_attr="best_decline_parameters")\
             ), to_attr="latest_scenario")\
        ).all()

class Well(Model):
    id = fields.IntField(pk=True)
    well_name = fields.CharField(max_length=255, unique=True, null=False)
    field_name = fields.CharField(max_length=255, null=False)
    created_on = fields.DatetimeField(null=False, auto_now=True)
    scenarios: fields.ReverseRelation["Scenarios"]

class Scenarios(Model):
    id = fields.IntField(pk=True)
    description = fields.CharField(max_length=1000, null=False)
    created_at = fields.DatetimeField(null=False, auto_now=True)
    #bunch of fields
    is_approved = fields.BooleanField(default=False)
    well : fields.ForeignKeyRelation[Well] = fields.ForeignKeyField("models.Well", related_name="scenarios")
    decline_parameters: fields.ReverseRelation["Decline_Parameters"]


class Decline_Parameters(Model):
    id = fields.IntField(pk=True)
    #bunch of fields
    label = fields.CharField(max_length=255, null=False)
    scenario: fields.ForeignKeyRelation[Scenarios] = fields.ForeignKeyField("models.Scenarios", related_name="decline_parameters")

The results that I am getting are the desired well list. however, only one well has a scenario. if I increase the limit on the scenarios to 2, then I get the two latest scenarios in accordance to the whole well list and not to each well!
How can I tweak the query to get the desired results?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the behavior with the shown Well, Scenarios, and Decline_Parameters models and the nested prefetch_related/Prefetch query. Trace how the scenario limit is applied across related Wells, then verify that the latest scenarios and best decline parameters are selected per Well rather than across the whole result set.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.