Open falled10 opened 2 years ago
This seems to be fixed.
The code snippet to test it:
from tortoise import Tortoise, fields, run_async
from tortoise.models import Model
class A(Model):
b = fields.ForeignKeyField("models.B")
class B(Model):
title = fields.CharField(max_length=255)
c = fields.ForeignKeyField("models.C")
class C(Model):
title = fields.CharField(max_length=255)
async def main():
await Tortoise.init( # type: ignore
db_url="sqlite://:memory:",
modules={"models": ["__main__"]},
)
await Tortoise.generate_schemas()
c = await C.create(title="test c")
b = await B.create(title="test b", c=c)
a = await A.create(b=b)
query = A.all().select_related("b__c").first()
print(query.sql())
a = await query
print(a.b.title) # works fine
print(a.b.c.title) # raises error AttributeError: 'QuerySet' object has no attribute 'title'
run_async(main())
the output:
SELECT "a"."b_id","a"."id","a__b"."c_id" "a__b.c_id","a__b"."title" "a__b.title","a__b"."id" "a__b.id","a__b__c"."title" "a__b__c.title","a__b__c"."id" "a__b__c.id" FROM "a" LEFT OUTER JOIN "b" "a__b" ON "a__b"."id"="a"."b_id" LEFT OUTER JOIN "c" "a__b__c" ON "a__b__c"."id"="a__b"."c_id" LIMIT 1
test b
test c
Nested join does not prefetch the object to nested object
Example