tortoise / tortoise-orm

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

Problems using `.raw()` instead of `.filter()` #1667

Open davidmcnabnz opened 5 days ago

davidmcnabnz commented 5 days ago

Describe the bug To get consistency between Tortoise-based and non-Tortoise based queries, I started replacing the usual <ModelClassName>.filter(...) fetches with <ModelClassName>.raw(<PlainSQLQueryString>).

It seemed to work, but I later found that saves were often not actually committing to the database. This caused some serious data consistency and workflow problems in several of my networks, which only came right after changing back to .filter(...).

Expected behavior When using <ModelClassName>.raw(...) I was expecting the returned Model row objects to behave in the same way as the ones returned from awaiting a <ModelClassName>.filter(...) query.

Additional context Is there a "safe" and recommended way to get Model row objects from raw SQL queries, which behave identically to those retrieved from .filter(...) ?

All help appreciated.

Abeautifulsnow commented 1 day ago

It seemed to work, but I later found that saves were often not actually committing to the database.

Hmm...What does that mean? Can you provide an example of how it works as you expect?