Comments (4)
Yeah, there are some shenanigans going on with count, because we calculate it without nesting query inside another select, and because of that we capture some bugs like this on when working with joins
Probably should just wrap all count requests inside outer select count(*)
You can try to make PR about it, or may be I will do it later
from tortoise-orm.
I looked at the definition of the queryset.CountQuery
class and did not understand how to do this in accordance with the existing rules. My subjective experience tells me to simply replace the body of the queryset.QuerySet.count()
method with the body of the example function sql_count()
. Since both approaches query the database with almost identical SQL, it does not look like an overhead, but returning the correct result.
With your permission, I leave this decision within your competence. Thank you for your time.
from tortoise-orm.
Should be fixed on 0.21.0
from tortoise-orm.
This is still broken from my testing:
Complete script to reproduce:
from datetime import datetime, timedelta
from tortoise import Model, run_async, Tortoise, connections, fields
from tortoise.queryset import QuerySet
class User(Model):
id: int = fields.IntField(pk=True)
username: str = fields.CharField(max_length=50, null=True, unique=True)
offers: fields.ReverseRelation["Offer"]
orders: fields.ReverseRelation["Order"]
class Offer(Model):
id: int = fields.IntField(pk=True)
name: str = fields.CharField(max_length=50, null=False)
user: fields.ForeignKeyRelation[User] = fields.ForeignKeyField(
"models.User", related_name="offers"
)
orders: fields.ReverseRelation["Order"]
class Order(Model):
created: datetime = fields.DatetimeField(null=False)
user: fields.ForeignKeyRelation["User"] = fields.ForeignKeyField(
"models.User", related_name="orders"
)
offer: fields.ForeignKeyRelation["Offer"] = fields.ForeignKeyField(
"models.Offer", related_name="orders"
)
async def sql_count(query: QuerySet[Model]) -> int:
_, result = await connections.get("default").execute_query(
f"SELECT count(*) AS total FROM ({query.sql()})"
)
return result[0]["total"]
async def main() -> None:
await Tortoise.init(db_url="sqlite://:memory:", modules={"models": ["__main__"]})
await Tortoise.generate_schemas()
businessman = await User.create(username="businessman")
offer1 = await Offer.create(name="offer1", user=businessman)
offer2 = await Offer.create(name="offer2", user=businessman)
offer3 = await Offer.create(name="offer3", user=businessman)
await User.create(username="client_one")
await User.create(username="client_two")
await User.create(username="client_three")
moment = datetime(2024, 1, 1, 0, 0, 0)
orders = []
for i, offer in enumerate((offer1, offer2, offer3)):
for j in range(3):
orders.append(Order(created=moment, user_id=(j + 2), offer=offer))
moment += timedelta(days=1)
await Order.bulk_create(orders)
query = (
User.filter(
orders__offer__user_id=1
).distinct()
)
result = await query
print(result)
print(await query.count())
print(await sql_count(query))
if __name__ == "__main__":
run_async(main())
Output:
[<User: 2>, <User: 3>, <User: 4>]
9
3
Versions:
pypika-tortoise==0.1.6
tortoise-orm==0.21.3
from tortoise-orm.
Related Issues (20)
- 0.21.0 throws "IntegrityError: NOT NULL constraint failed" due to usage of 'pk' to indicate primary key HOT 3
- max recursion bug
- Feature: expose `timezone` and `use_tz` params in RegisterTortoise class HOT 5
- OperationalError while bulk updating the tortoise model. HOT 1
- [BUG] get_or_create function raises "Object does not exist" HOT 1
- DoesNotExist can be more useful HOT 2
- There is an entry for table "table", but it cannot be referenced from this part of the query HOT 2
- Unable to run unit tests using tortoise-orm initializer() with a postgres database HOT 6
- Is there a django similar scheme to generate model files based on the structure of the existing database python manage.py inspectdb > models.py HOT 1
- unique indexes (for unique_together) on nullable columns need to be optionable for nulls not distinct HOT 3
- [Bug] Failed to set schema for models.Model
- Custom generated primary key (Snowflake) ignored, a serial is saved in the backend HOT 2
- Have you considered allowing Tortoise to be independently initialized multiple times in different packages? HOT 3
- Generic relation field HOT 1
- Race condition bug in ConnectionWrapper HOT 1
- I integrated tortoise-orm in FastAPI, but it reported an error when started in debug mode
- KeyError Issue HOT 10
- Problems using `.raw()` instead of `.filter()` HOT 1
- Model.update_or_create should inherit the value of model HOT 5
- Tortoise ORM fails to generate tables for non-default schemas HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tortoise-orm.