tortoise / tortoise-orm

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

Unable to get underlying object from Queryset #212

Closed horlahlekhon closed 5 years ago

horlahlekhon commented 5 years ago

Hi, i cant seem to be able to get the underlying object when i either get or filter what i get back is a QuerySet.

tortoise-orm version : 0.13.12

python version : 3.7

example:

Messages.filter(Q(message_id=message_id)).first()
Messages.get(message_id=message_id)

this was supposed to return the underlying message object according to the documentation, but i still get query set.

please what am i doing wrong.

grigi commented 5 years ago

You need to await the Queryset to get the objects. Tortoise is an async framework, and we can only do I/O when you cede control of the I/O loop.

So do an await Messages.filter(message_id=message_id).first() instead.

grigi commented 5 years ago

If the documentation is not making it clear enough that one has to use await could you please point me to the docs you looked at so I can make it clearer?

horlahlekhon commented 5 years ago

Thanks very much, i didnt wait it , it has retruned the object now.

on a side note , is there a way to use alembic with tortoise-orm or is there a migration package. thanks.

grigi commented 5 years ago

A pleasure.

Unfortunately Alembic is very SQLAlchemy specific. We don't currently have a formal migration system in place for Tortoise-ORM. There is an open ticket #8 and a workaround here: https://github.com/tortoise/tortoise-orm/issues/8#issuecomment-534946871

The plan is to get Tortoise-ORM feature complete to a production level (v1.0, see #19) and then we will look at the next round of work, which is likely to include migrations, better SerDes...