Repository navigation
Support model instances in forward relation membership filters - #2323
Conversation
Merging this PR will not alter performance
|
Compare against the integer id column so PostgreSQL can infer the parameter type. Add a second row to verify the annotation selects only the intended relation.
|
@IlyaKhalafi I tried the following code snippet: from tortoise import Tortoise, fields, run_async
from tortoise.models import Model
class Event(Model):
name = fields.TextField()
class Address(Model):
city = fields.CharField(max_length=64)
event: fields.OneToOneRelation[Event] = fields.OneToOneField(
Event, related_name="address", primary_key=True
)
async def run():
await Tortoise.init(db_url="sqlite://:memory:", modules={"models": ["__main__"]})
await Tortoise.generate_schemas()
event = await Event.create(name="Test")
address = await Address.create(city="Santa Monica", event=event)
print(await Event.filter(address__city__in=[address.city]))
print("-" * 20)
try:
print(await Event.filter(address__in=[address]))
except Exception as e:
print(e)
if __name__ == "__main__":
run_async(run())And got: Is this the expected result? |
|
No, that should return the event. I reproduced the same failure on This PR covers the forward direction, where |
Agreed. |
|
Opened #2338 for the reverse lookup fix, with regression coverage for your example and renamed columns. |
|
I updated |
Description
This PR adds in and not_in filters on forward relations using model instances. With this, callers can filter related objects directly, including relations that reference a non-primary unique field.
Fixes #1819
Motivation and Context
Forward relation membership currently treats the lookup as a nested field name. The new lookup uses the existing source-field encoder and operators, preserving annotation filters and invalid lookup errors.
How Has This Been Tested?
Twenty regression cases fail on the original code. The full SQLite suite passes with 1997 passed, 149 skipped and two expected failures.
make checkpasses, including formatting, lint, types, security scanning and package validation. The 23 focused tests also pass on PostgreSQL 18 using both asyncpg and psycopg. MySQL and MSSQL were not run locally.Checklist