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 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
I would certainly not recommend it, but if you really want / must, change you class definition into HeroTeamView(SQLModel, table=True). You might have to add __tablename__ = "viewname" and define a primary key to satisfy sqlalchemy.
SQLModel is SQLAlchemy declarative_base, so you often find the solution by searching for that instead. This SO answer is informative: https://stackoverflow.com/a/53253105
Has an example that uses SQLAlchemy-utils, based on an example there - this includes the view definition part also:
Haven't tested these, we considered and it's still an option, but we now just try by filtering some queries and relationships instead as the case is pretty simple.
@byrman - why would you not recommend what you say? AFAIK the solution I mentioned above boils down to the same pretty much? Maybe the lib impl there actually does more though.
To clarify, I tried to map a view directly on a single HeroTeamView model. This actually works, because for the generated SQL it doesn't matter whether it selects from a table or a view. Your proposed solution, @antont, looks promising / better, but I'm still struggling with it.
__tablename__ = "viewname" (or did you mean __table__ ?)
The latter gave me a runtime error: AttributeError: 'str' object has no attribute 'c'!?
And if it works, why would you not recommend it?
Personally, I would prefer to interact with individual Hero and Team objects in my code, navigating relationships via properties. Such a HeroTeamView 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.
Reacted by Toni Alatalo, Ivan C., Kiriti Badam, g13n4, João Flávio Santos, Ihsan Topaloglu, David and Francesco Bruzzesi
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.
There is this , which is maybe useful to make an SQLModel implementation not sure. I would be happy for a proper view table in SQLModel, for example for use with auto-generating migrations and hiding columns when not wanting to allow access to the underlying table.
Este último me deu um erro de tempo de execução: !?AttributeError: 'str' object has no attribute 'c'
E se funciona, por que você não recomendaria?
Pessoalmente, prefiro interagir com indivíduos e objetos em meu código, navegando em relacionamentos por meio de propriedades. Isso não me parece muito natural. Contar equipes se torna menos fácil, ferramentas de migração podem reclamar, etc. Mas pode haver um caso de uso para isso, por exemplo, quando você não tem permissões nas tabelas individuais, mas tem permissão para consultar a exibição.Hero``Team``HeroTeamView
Another use-case is processing the results of a complex SQL query as a Python object.
Sometimes quite complex SQL queries are created to extract information from numerous tables and it's not practical or feasible to repeat the same effort in SQLModel but there's a need to process the query results and having it defined as a Pydantic model really helps.
@riziles , in a way it doesn't matter, it's either a complex SQL query being loaded from a text file or just querying a view.
Some users just need a simple way to load the result of a query into python or Pydantic objects but it's not feasible for an ORM library to support every use-case. I personally use SQLAlchemy text statements with dataclasses as a workaround but it brings a lot of boilerplate. It is appealing to just create a view and tie that to a class and just pulling the data that way.
Just FYI, the use cases for views extend beyond simply joining existing models. Views are a valid tool in abstraction and, in particular, materialized views are an important performance tool.
Our use case is a table that has "effective dating" of rows, but 99% of the time we only want the "current" version of something. In our case there are efficiency gains to create a materialized view, add any independent indexes and map to that with the table=True option as shown in examples above.
That being said, it would be nice to add readonly support to a "table" model so the framework knows not to attempt to mutate it, typing hints are updated, and it's obvious to the next developer.
Reacted by David, Andrew Fergusson, Mat, Craig Hills, Raphael Mitsch, annececiledunand and Tim Rabl
Activity
byrman commentedon Mar 2, 2022
I would certainly not recommend it, but if you really want / must, change you class definition into
HeroTeamView(SQLModel, table=True). You might have to add__tablename__ = "viewname"and define a primary key to satisfy sqlalchemy.antont commentedon Mar 2, 2022
SQLModel is SQLAlchemy declarative_base, so you often find the solution by searching for that instead. This SO answer is informative: https://stackoverflow.com/a/53253105
Has an example that uses SQLAlchemy-utils, based on an example there - this includes the view definition part also:
There's also https://pypi.org/project/sqlalchemy-views/ but I understood that it does not have ORM support, like SQLAlchemy-utils does.
Haven't tested these, we considered and it's still an option, but we now just try by filtering some queries and relationships instead as the case is pretty simple.
@byrman - why would you not recommend what you say? AFAIK the solution I mentioned above boils down to the same pretty much? Maybe the lib impl there actually does more though.
byrman commentedon Mar 2, 2022
Wasn't the question about how to do it using SQLModel on an existing view?
byrman commentedon Mar 2, 2022
To clarify, I tried to map a view directly on a single
HeroTeamViewmodel. This actually works, because for the generated SQL it doesn't matter whether it selects from a table or a view. Your proposed solution, @antont, looks promising / better, but I'm still struggling with it.antont commentedon Mar 3, 2022
@byrman you are right, my answer is different that way - it includes both parts.
AFAIK the mapping of the class to the view is:
__table__ = create_view(Which I think ends up being the same as what you said:
__tablename__ = "viewname"(or did you mean__table__?)But in my case just to a new view defined there.
Maybe the lib does extra though, as there is also the
metadata=Base.metadata? Or perhaps that's not necessary when dealing with an existing view.Am curious about whether your solution indeed just works, or if something else is needed. And if it works, why would you not recommend it?
byrman commentedon Mar 3, 2022
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
HeroandTeamobjects in my code, navigating relationships via properties. Such aHeroTeamViewdoesn'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.antont commentedon Mar 7, 2022
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==Nonechecks in queries and relationships. We'll see whether stick with that or start using a view later.methuselah-0 commentedon Mar 10, 2024
There is this , which is maybe useful to make an SQLModel implementation not sure. I would be happy for a proper view table in SQLModel, for example for use with auto-generating migrations and hiding columns when not wanting to allow access to the underlying table.
giovanni-bellini-argo commentedon Apr 18, 2024
any update on view support in sqlmodel? 👀
joaoflaviosantos commentedon Apr 19, 2024
@byrman Thank you very much! 🙏
itopaloglu83 commentedon Dec 8, 2024
Another use-case is processing the results of a complex SQL query as a Python object.
Sometimes quite complex SQL queries are created to extract information from numerous tables and it's not practical or feasible to repeat the same effort in SQLModel but there's a need to process the query results and having it defined as a Pydantic model really helps.
riziles commentedon Dec 9, 2024
@itopaloglu83 , if you are not issuing a create statement, why does it matter if the underlying sql object is a view or a table?
itopaloglu83 commentedon Dec 9, 2024
@riziles , in a way it doesn't matter, it's either a complex SQL query being loaded from a text file or just querying a view.
Some users just need a simple way to load the result of a query into python or Pydantic objects but it's not feasible for an ORM library to support every use-case. I personally use SQLAlchemy text statements with dataclasses as a workaround but it brings a lot of boilerplate. It is appealing to just create a view and tie that to a class and just pulling the data that way.
riziles commentedon Dec 9, 2024
@itopaloglu83, I'm not sure I understand what you are saying. Why can't you use a SQLModel table on a view?
This works, and presumably the user doesn't need to actually execute creates and deletes every single time they run:
mbyrne00 commentedon Jan 20, 2025
Just FYI, the use cases for views extend beyond simply joining existing models. Views are a valid tool in abstraction and, in particular, materialized views are an important performance tool.
Our use case is a table that has "effective dating" of rows, but 99% of the time we only want the "current" version of something. In our case there are efficiency gains to create a
materialized view, add any independent indexes and map to that with thetable=Trueoption as shown in examples above.That being said, it would be nice to add readonly support to a "table" model so the framework knows not to attempt to mutate it, typing hints are updated, and it's obvious to the next developer.