How to get row count of session.exec result #1487
-
First Check
Commit to Help
Example Codefrom sqlmodel import Field, Session, SQLModel, create_engine, select
class Hero(SQLModel, table=True):
id: int | None = Field(default=None, primary_key=True)
name: str
sqlite_file_name = "database.db"
sqlite_url = f"sqlite:///{sqlite_file_name}"
engine = create_engine(sqlite_url, echo=True)
def create_db_and_tables():
SQLModel.metadata.create_all(engine)
def create_heroes():
hero_1 = Hero(name="Deadpond")
hero_2 = Hero(name="Spider-Boy")
hero_3 = Hero(name="Rusty-Man")
with Session(engine) as session: #
session.add(hero_1) #
session.add(hero_2)
session.add(hero_3)
session.commit()
row_counts = session.exec(select(Hero)).count() # HERE! How can I get the row counts? In my code I had where as well.
print(f"row_counts: {row_counts}")
def main():
create_db_and_tables()
create_heroes()
if __name__ == "__main__":
main() Description
Operating SystemLinux Operating System DetailsUbuntu 21.10 SQLModel Version0.0.6 Python Version3.10.2 Additional ContextNone |
Beta Was this translation helpful? Give feedback.
Replies: 12 comments 1 reply
-
Try this way (copied from my test project): from sqlalchemy import func
session.exec(select([func.count(Users.email)]).where(Users.email == res.email)).one() |
Beta Was this translation helpful? Give feedback.
-
hi @mgurg Thanks for your interest. I have multiple filters as you have one. Is using just single column in |
Beta Was this translation helpful? Give feedback.
-
You have to figure out this by yourself, I'm not an expert here. I just found in the past solution for a similar problem for myself. |
Beta Was this translation helpful? Give feedback.
-
Ideally, you would would not have to specify a column at all and use |
Beta Was this translation helpful? Give feedback.
-
Just an update that @mgurg's suggestion of using
|
Beta Was this translation helpful? Give feedback.
-
Can confirm; following statement is working: # simple
session.exec(select(func.count(User.user_id))).one()
# with .where() statement
session.exec(
select(func.count(User.user_id)).where(
User.last_action >= datetime.today() - timedelta(days=1)
)
).one() |
Beta Was this translation helpful? Give feedback.
-
Hi, @jplacht your approach works, the only issue with it is that types are incorrect, I know we can ignore it, but I was wondering if there is a better way to use count = (await session.exec(select(func.count(ItemModel.id)))).first() which gives me this error :
so I modified it a little to get rid of the mypy errors: count = (await session.exec(select(func.count()).select_from(ItemModel))).first() |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
it's work fine in sqlmodel |
Beta Was this translation helpful? Give feedback.
-
To get rid of the mypy (or pylance/pyright) type issues this is working ok: from sqlmodel import Session, col, func, select
from .database import engine
from .models import Page
with Session(engine) as session:
count = session.exec(select(func.count(col(Page.id)))).one() |
Beta Was this translation helpful? Give feedback.
-
if suppose this query returns 1 million row, will not it make it need big processing power of cpu, or perhaps it crash my os. i means to know which way is less cpu usage to get the row count? |
Beta Was this translation helpful? Give feedback.
-
this is what GPT says, generally speaking, in modern DB, it's the same performance:
|
Beta Was this translation helpful? Give feedback.
To get rid of the mypy (or pylance/pyright) type issues this is working ok: