tortoise / tortoise/tortoise-orm
__in filter modifier ignore None value
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 have a model Player with optional ForeignKey field team. I want to get all the players who are on team 1 or not on any team. But if I use a filter team_id__in=[team1.id, None], it returns only those players who are in the team 1
To Reproduce
from tortoise import Tortoise, fields, run_async
from tortoise.exceptions import NoValuesFetched
from tortoise.models import Model
class Team(Model):
id = fields.IntField(pk=True)
players: fields.ReverseRelation["Player"]
class Player(Model):
id = fields.IntField(pk=True)
team: fields.ForeignKeyRelation[Team] = fields.ForeignKeyField(
"models.Team", related_name="players", null=True, default=None
)
nick = fields.CharField(max_length=10)
def __repr__(self):
return f"Player(nick={self.nick})"
async def run():
await Tortoise.init(db_url="sqlite://:memory:", modules={"models": ["__main__"]})
await Tortoise.generate_schemas()
team1 = await Team.create(id=1)
team2 = await Team.create(id=2)
p1 = await Player.create(team=team1, nick="p1")
p2 = await Player.create(team=team1, nick="p2")
p3 = await Player.create(team=team1, nick="p3")
p4 = await Player.create(team=team1, nick="p4")
p5 = await Player.create(team=team2, nick="p5")
p6 = await Player.create(team=team2, nick="p6")
p7 = await Player.create(team=team2, nick="p7")
p8 = await Player.create(nick="p8")
p9 = await Player.create(nick="p9")
res = await Player.filter(team_id__in=[team1.id, None])
print(res) # => [Player(nick=p1), Player(nick=p2), Player(nick=p3), Player(nick=p4)]
print(len(res)) # => 4
if __name__ == "__main__":
run_async(run())
Expected behavior
Query team_id__in=[team1.id, None] returns all the players who are on team 1 or not on any team.
res = await Player.filter(team_id__in=[team1.id, None])
print(res) # => [Player(nick=p1), Player(nick=p2), Player(nick=p3), Player(nick=p4), Player(nick=p8), Player(nick=p9)]
print(len(res)) # => 6
I know that i can use something like this
from tortoise.query_utils import Q
res = await Player.filter(Q(team_id__isnull=True) | Q(team_id=team1.id))
and i'm not sure if this is a bug or not, but it would be great if the __in modifier worked the same way as in in Python
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the Player.filter(team_id__in=[team1.id, None]) entry point and trace how the __in modifier is translated for the SQLite example. Compare the generated query behavior with the Q(team_id__isnull=True) | Q(team_id=team1.id) alternative. Done means the example returns all six matching players, including those with no team.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python, sqlite
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100