Skip to content

A question about filter queries, is it unsupported or a bug? #2003

@cmdragon

Description

@cmdragon

tortoise-orm == 0.24.0

tags = fields.ManyToManyField(
        'blog.Tags',
        related_name='blogs',
        description='tag',
        through="blog_blog_tags",
        forward_key="tags_id", 
        backward_key="blog_id"  
    )

I have a simple query requirement that needs to retrieve data with tag ids 469 and 7107 simultaneously, but there is a prerequisite condition: only need to query simultaneously when new is true.

Initially, the code was like this

blog_queryset = Blog.filter(tags__id=469)
if new:
    blog_queryset.filter(tags__id=7107)

But I found that the query results are empty,When handling many-to-many relationships, using .filter(tags__id=X).filter(tags__id=Y) consecutively does not produce an intersection query.

Then I tried to query using Q expressions.

query.filter(Q(tags__id=469) & Q(tags__id=7107))

I found that the result is still empty. But strangely, when I changed & to and, I was able to get the correct query results, which were no longer empty.

query.filter(Q(tags__id=469) and Q(tags__id=7107))

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions