How to query View in sqlmodel
See original GitHub issueFirst Check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue 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
from typing import Optional
from sqlmodel import Field, Session, SQLModel, create_engine, select
class HeroTeamView(SQLModel):
name: str
secret_name: str
age: Optional[int] = None
sqlite_file_name = "my.db"
db_url = f"mysql+mysqldb://{db_user}:{db_password}@{db_host}:{db_port}/{db_name}"
engine = create_engine(db_url, echo=True)
with Session(engine) as session:
statement = select(HeroTeamView)
orgs = session.exec(statement)
print(f"orgs::{orgs}")
org_list = orgs.fetchall()
Description
I have a view(Lets say HeroTeamView) created in mysql db. I want to read this. This view is essentially a left join of Hero and Teams table Joined on Hero.Id
.
As shown in example above as soon as I try to select This view I get error
HeroTeamView
is not a 'SQLModelMetaclass' object is not iterable
I am not quiet sure I understand how to access rows created by view
Any pointers appreciated
Operating System
Windows
Operating System Details
No response
SQLModel Version
0.06
Python Version
3.9.7
Additional Context
I dont want to use Hero and Team tables directly to write a select query as there are multiple tables and joins in “real” world problem for me. Using Views provides me some obvious benefits like mentioned here
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (4 by maintainers)
Top Results From Across the Web
Read Data - SELECT - SQLModel
Let's now see how to read data from the database! ... Before writing Python code let's do a quick review of how querying...
Read more >How to query View in sqlmodel - Stack Overflow
from typing import Optional from sqlmodel import Field, Session, SQLModel, create_engine, select class HeroTeamView(SQLModel): name: str ...
Read more >Using the SQL Model Classes | Qt SQL 6.4.1 - Qt Documentation
This is explained in detail in the Presenting Data in a Table View section. ... QSqlQueryModel offers a read-only model based on an...
Read more >SQL models - dbt Developer Hub
When you execute dbt run , dbt will build this as a view named ... What happens if the SQL in my query...
Read more >Get Information About a View - SQL Server | Microsoft Learn
Specifies that additional options for views based on aggregate queries are available. Output All Columns Shows whether all columns are returned ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I created a view in PostgreSQL:
Queried it like this:
And got this output:
The latter gave me a runtime error:
AttributeError: 'str' object has no attribute 'c'
!?Personally, I would prefer to interact with individual
Hero
andTeam
objects in my code, navigating relationships via properties. Such aHeroTeamView
doesn’t feel very natural to me. Counting teams becomes less easy, migration tools might complain, etc. But there might be a use case for this, for example when you don’t have permissions on the individual tables but are allowed to query the view.Right-o, thanks for the info @byrman . We’ve been also happy with relationships, have not planned multi table views.
We looked into using a view to implement the trashcan pattern for a kind of soft delete, like in https://michaeljswart.com/2014/04/implementing-the-recycle-bin-pattern-in-sql/
After some consideration, we didn’t try views (yet), but have just now
deleted==None
checks in queries and relationships. We’ll see whether stick with that or start using a view later.