You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I used the GitHub search to find a similar question and didn't find it.
I searched the SQLModel documentation, with the integrated search.
I already searched in Google "How to X in SQLModel" and didn't find any information.
I already read and followed all the tutorial in the docs and didn't find an answer.
I already checked if it is not related to SQLModel but to Pydantic.
I already checked if it is not related to SQLModel but to SQLAlchemy.
Commit to Help
I commit to help with one of those options 👆
Example Code
session.query(MyModel) # <= this is deprecatedsession.query(MyModel).all() # <= this returns all recordssesssion.exec(statement)
# ^ This requires a statement to be constructed ahead which makes it # difficult for composition in certain cases
Description
Session#query has been deprecated but its an incredibly useful part of the API. My use case is that I want to have methods that do the following: MyModel.query(optional_session).all()
With the current query method, I can do this very easily. With the new API, there's no way to do this.
The reason given for the old API is that types break, but you can easily specify Query[T] as the return type and all is solved. Here's a complete example:
fromsqlalcemy.ormimportQueryclassMagicSQLModel(SQLModel):
@classmethoddefquery(cls: type[T], session: Session|None=None) ->Query[T]:
ifnotsession:
withdb_session() ass:
session=sreturnsession.query(cls)
classMyModel(MagicSQLModel, table=True):
# ....MyModel.query().all() # <= This is typed properly
With the new API I'd have to create an intermediary object which will be perpetually incomplete:
# Note there are minor errors (e.g. exists)classAutoQuery(Generic[T]):
def__init__(self, model: type[T], session: Session):
"""Initialize with a shared session."""self.model=modelself.session=sessionself.statement=select(model)
defwhere(self, *conditions) ->"AutoQuery[T]":
"""Apply WHERE conditions to the query."""self.statement=self.statement.where(*conditions)
returnselfdefall(self) ->list[T]:
"""Execute and return all results."""returnself.session.exec(self.statement).all()
deffirst(self) ->T|None:
"""Execute and return the first result."""returnself.session.exec(self.statement).first()
deflast(self) ->T|None:
"""Execute and return the last result."""returnself.session.exec(self.statement.order_by(self.model.id.desc())).first()
defcount(self) ->int:
"""Execute and return the count of results."""returnself.session.exec(self.statement).count()
deffind(self, id: int) ->T|None:
"""Execute and return a single result by ID."""returnself.session.exec(self.statement.where(self.model.id==id)).first()
defexists(self) ->bool:
"""Check if any record matches the query."""returnself.session.exec(self.statement.exists()).scalar()
deforder(self, *columns: str) ->"AutoQuery[T]":
"""Sort results by specified columns."""self.statement=self.statement.order_by(*columns)
returnselfdeflimit(self, n: int) ->"AutoQuery[T]":
"""Limit the number of results returned."""self.statement=self.statement.limit(n)
returnselfdefoffset(self, n: int) ->"AutoQuery[T]":
"""Skip the first `n` results."""self.statement=self.statement.offset(n)
returnselfdefdelete(self) ->int:
"""Delete matching records and return affected row count."""stmt=delete(self.model).where(*self.statement.whereclause)
result=self.session.exec(stmt)
self.session.commit()
returnresult.rowcountclassMagicSQLModel(SQLModel, table=True)
@classmethoddefquery(cls: type[T], session=None) ->AutoQuery[T]: # this should be implemented in your new base classwithSession(engine) ass:
session=sreturnAutoQuery(cls, session)
Operating System
macOS
Operating System Details
This isn't related
SQLModel Version
0.0.22
Python Version
3.10.13
Additional Context
This is just a simple question about the deprecation of Session#query
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
First Check
Commit to Help
Example Code
Description
Session#query has been deprecated but its an incredibly useful part of the API. My use case is that I want to have methods that do the following:
MyModel.query(optional_session).all()
With the current query method, I can do this very easily. With the new API, there's no way to do this.
The reason given for the old API is that types break, but you can easily specify Query[T] as the return type and all is solved. Here's a complete example:
With the new API I'd have to create an intermediary object which will be perpetually incomplete:
Operating System
macOS
Operating System Details
This isn't related
SQLModel Version
0.0.22
Python Version
3.10.13
Additional Context
This is just a simple question about the deprecation of Session#query
Beta Was this translation helpful? Give feedback.
All reactions