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
fromsqlmodelimportField, SQLModel, Session, col, selectfromsqlalchemyimportfuncclassPerson(SQLModel, table=True):
id: int|None=Field(default=None, primary_key=True)
first_name: strlast_name: strage: int|None=None# Note: this is NOT a tableclassQueryResult(SQLModel):
full_name: strperson_age: int|None=NonewithSession(engine) assession:
query= (
select(
func.concat(Person.first_name, Person.last_name).label('full_name'),
col(Person.age).label('person_age')
)
.select_from(Person)
)
results=session.exec(query).all()
print(results)
Description
Supposed that I have 2 models, Person for a table and QueryResult to map the result. A simple query is to select some people and concatenate their first and last name into name.
The results is a list of tuples. However, I want it to be a list of QueryResult. I've made sure that the label and field names are matched. But what else do I need to do to map the result to the defined class?
I know that I can do it manually by looping through the results, but it does not feel right to me. SQLAlchemy or SQLModel must have a solution for this, or?
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
Supposed that I have 2 models,
Person
for a table andQueryResult
to map the result. A simple query is to select some people and concatenate their first and last name into name.The
results
is a list of tuples. However, I want it to be a list ofQueryResult
. I've made sure that thelabel
and field names are matched. But what else do I need to do to map the result to the defined class?I know that I can do it manually by looping through the
results
, but it does not feel right to me.SQLAlchemy
orSQLModel
must have a solution for this, or?Operating System
macOS
Operating System Details
No response
SQLModel Version
0.0.22
Python Version
3.11.4
Additional Context
No response
Beta Was this translation helpful? Give feedback.
All reactions