tortoise / tortoise-orm

Familiar asyncio ORM for python, built with relations in mind
https://tortoise.github.io
Apache License 2.0
4.58k stars 378 forks source link

Query set method only() doesn't work with values() #1511

Closed Prof1-web closed 11 months ago

Prof1-web commented 11 months ago

Describe the bug Query set api https://tortoise.github.io/query.html#tortoise.queryset.QuerySet.only only() doesn't work with values()

To Reproduce

class Request(Model):
    id = fields.BigIntField(pk=True)
    first_name = fields.CharField(255)
    last_name = fields.CharField(255, null=True)
    user = fields.ForeignKeyField("models.User")
    approved = fields.BooleanField(default=False)
    created_at = fields.DatetimeField(auto_now_add=True)

print(Request.all().only("id").sql())
print(Request.all().only("id").values().sql())
# SELECT "id" "id" FROM "request"
# SELECT "id" "id","first_name" "first_name","last_name" "last_name","approved" "approved","created_at" "created_at","user_id" "user_id" FROM "request"

Expected behavior I expect that first and second sql queries to be equal, but in second case tortoise loads all the fields from the db

Prof1-web commented 11 months ago

Ok, I found solution:

print(Request.all().values("id").sql())

It works, but documentation needs to be changed, it's not obvious