sqlmodel
                                
                                 sqlmodel copied to clipboard
                                
                                    sqlmodel copied to clipboard
                            
                            
                            
                        SAWarning from sqlalchemy
First Check
- [X] I added a very descriptive title to this issue.
- [X] I used the GitHub search to find a similar issue and didn't find it.
- [X] I searched the SQLModel documentation, with the integrated search.
- [X] I already searched in Google "How to X in SQLModel" and didn't find any information.
- [X] I already read and followed all the tutorial in the docs and didn't find an answer.
- [X] I already checked if it is not related to SQLModel but to Pydantic.
- [X] I already checked if it is not related to SQLModel but to SQLAlchemy.
Commit to Help
- [X] I commit to help with one of those options 👆
Example Code
No code
Description
Every time I use the SQLModel ORM, I get this warning.
/.../sqlmodel/orm/session.py:60: SAWarning: Class SelectOfScalar will not make use of SQL compilation caching as it does 
not set the 'inherit_cache' attribute to ``True``.  This can have significant performance implications including some 
performance degradations in comparison to prior SQLAlchemy versions.  Set this attribute to True if this object can make 
use of the cache key generated by the superclass.  Alternatively, this attribute may be set to False which will disable this 
warning. (Background on this error at: https://sqlalche.me/e/14/cprf)
  results = super().execute(
Operating System
Linux
Operating System Details
Linux 5.15.7-arch1-1 x86_64 GNU/Linux
SQLModel Version
0.0.4
Python Version
Python 3.9.9
Additional Context
The only other reference to this problem that I have found is this one https://github.com/tiangolo/sqlmodel/issues/129#issuecomment-991074972
I've solved mine by importing select() from sqlalchemy directly.
I've solved mine by importing select() from sqlalchemy directly.
Be aware that if you select a single value, you will still get a tuple in return, e.g.:
# with 'select' from sqlmodel
with Session(engine) as session:
    count_statement = select(func.count()).select_from(Book).where(Book.release_year < 1960)
    amount = session.exec(count_statement).first()
    assert amount == 2
This needs to be assert amount == (2, ) if you use the select from sqlalchemy.
Is importing select() from sqlalchemy really a solution to this, it don't see the point of using SQLModel and then bypassing it on all select statements. ? Is there a parameter/settings that need to be set somewhere to activating the caching ?
While i can't tell you why, the following amendment resolves this warning for me:
# raises the SAWarning
session.query(MyModel).where(MyModel.field1 == 'value').first()
# Does not raise the warning
from sqlmodel import col
session.query(MyModel).where(col(MyModel.field1) == 'value').first()
Is importing select() from sqlalchemy really a solution to this, it don't see the point of using SQLModel and then bypassing it on all select statements. ? Is there a parameter/settings that need to be set somewhere to activating the caching ?
Did you find an answer?
While i can't tell you why, the following amendment resolves this warning for me:
# raises the SAWarning session.query(MyModel).where(MyModel.field1 == 'value').first() # Does not raise the warning from sqlmodel import col session.query(MyModel).where(col(MyModel.field1) == 'value').first()
What happens when you don't need a col? Like in :
with Session(engine) as session:
  stmt = select(MyModel)
  results = session.exec(stmt).all()
I haven't found any way to disable caching for now.
And the same issue occurs even when not using col.
I am having the same issue. Fix needed ASAP.
Same issue
Same here :)
Same issue...
Hi everyone, please stop replying with "same" without contributing more info, that is what the 👍 button at the top is for. Doing so generates spam for everyone who is subscribed, and wastes the valuable time of people like tiangolo.
Using the 👍 button in this way is generally considered good etiquette across all of GitHub. Thanks, and I also hope that someone comes up with a solution soon. 😃
EDIT: LOL, what is going on here? Is this GitHub or Facebook? I'm not sure whether or not @mmlynarik is trolling. :joy: I'm just subscribed to this thread, hoping for a solution, and getting mildly disappointed each time I receive a notification for a pointless message. I'm not particularly motivated to try and solve this issue myself. I do however have a suggestion...
If you are so eager for tiangolo to fix this issue, then rather than spamming him, please consider showing him some :heart: with the Sponsor button at the bottom. I'd really love to see FOSS maintainer become a more viable career choice.
Hi everyone, please stop replying with "same" without contributing more info, that is what the 👍 button at the top is for. Doing so generates spam for everyone who is subscribed, and wastes the valuable time of people like tiangolo.
Using the 👍 button in this way is generally considered good etiquette across all of GitHub. Thanks, and I also hope that someone comes up with a solution soon. 😃
@maresb do you know what is the reason for the warning? Maybe @tiangolo could drop here some hint what could have gone wrong...
Hi everyone, please stop replying with "same" without contributing more info, that is what the 👍 button at the top is for. Doing so generates spam for everyone who is subscribed, and wastes the valuable time of people like tiangolo.
Using the 👍 button in this way is generally considered good etiquette across all of GitHub. Thanks, and I also hope that someone comes up with a solution soon. 😃
EDIT: LOL, what is going on here? Is this GitHub or Facebook? I'm not sure whether or not @mmlynarik is trolling. 😂 I'm just subscribed to this thread, hoping for a solution, and getting mildly disappointed each time I receive a notification for a pointless message. I'm not particularly motivated to try and solve this issue myself. I do however have a suggestion...
If you are so eager for tiangolo to fix this issue, then rather than spamming him, please consider showing him some ❤️ with the Sponsor button at the bottom. I'd really love to see FOSS maintainer become a more viable career choice.
You can check the list of sponsors, you will find my name on it.
Not a long term solution, but if you want to filter out the error for now you can use the following snippet:
import warnings
warnings.filterwarnings("ignore", ".*Class SelectOfScalar will not make use of SQL compilation caching.*")
I would be happy to set the mentioned attribute inherit_cache to get rid of the warning. How would I do that?
Following the link in the error, https://sqlalche.me/e/14/cprf, gives some background, but no instruction on where or how to set the attribute.
The solution I found was that the inherit_cache field should be set in the Select and SelectOfScalar classes of sqlmodel.sql.expression.

Personally, while it is fixed, I will set that field in my code in the following way.
from sqlmodel.sql.expression import Select, SelectOfScalar
SelectOfScalar.inherit_cache = True  # type: ignore
Select.inherit_cache = True  # type: ignore
Just a friendly ping to @tiangolo.
The solution I found was that the
inherit_cachefield should be set in theSelectandSelectOfScalarclasses ofsqlmodel.sql.expression.
Personally, while it is fixed, I will set that field in my code in the following way.
from sqlmodel.sql.expression import Select, SelectOfScalar SelectOfScalar.inherit_cache = True # type: ignore Select.inherit_cache = True # type: ignore
ModuleNotFoundError: No module named 'sqlmodel'
Does anyone have an actual solution?
I tried to install the dependency myself, but this does not lead to a solution, then I gave the opportunity to install PyCharm but the result is the same
I've solved mine by importing select() from sqlalchemy directly.
have effect side, [{'a': {}}]
Thanks for the report! This was solved in https://github.com/tiangolo/sqlmodel/pull/234, it will be available in SQLModel 0.0.7, released in the next hours.
If some of you would like to help me maintain SQLModel, it would be much appreciated! What I need help with the most is answering questions from others in issues. Thanks! :cake:
I just upgraded to SQLModel 0.0.7 (and SQLAlchemy 1.4.40) and still get this warning. Am I the only one?
Yeah, this is not working for me either. (Maybe that's why the bug was reopened?)
 
 
                                    
                                    
                                    
                                
It seems the fix was only applied to expression.py.jinja2, not expression.py
Not sure if that was intentional.
It seems the fix was only applied to
expression.py.jinja2, notexpression.pyNot sure if that was intentional.
I had only changed the jinja one due to this line at the top of the python file.
# WARNING: do not modify this code, it is generated by expression.py.jinja2
I guess that makes sense. I have no idea how exactly the release works here. It looks to me like generate_select.py was supposed to have been executed at some point but wasn't.
Ah! Thanks everyone, good point @rabinadk1, I fixed it in https://github.com/tiangolo/sqlmodel/pull/422, I also made a test that ensures the file is up to date with the Jinja2 template.
This will be available in the next version, in some hours. SQLModel 0.0.8. :rocket:
Can this be closed?
@nickatnight Yes, this is now working as expected. The warning is no longer shown.