Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SAWarning from sqlalchemy #189

Open
8 tasks done
poupryc opened this issue Dec 13, 2021 · 28 comments · Fixed by #234
Open
8 tasks done

SAWarning from sqlalchemy #189

poupryc opened this issue Dec 13, 2021 · 28 comments · Fixed by #234
Labels
bug Something isn't working

Comments

@poupryc
Copy link

poupryc commented Dec 13, 2021

First 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

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(

A link is given.

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
#129 (comment)

@poupryc poupryc added the question Further information is requested label Dec 13, 2021
@postpersonality
Copy link

I've solved mine by importing select() from sqlalchemy directly.

@BurnySc2
Copy link

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.

@mfrey777
Copy link

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 ?

@chriswhite199
Copy link
Contributor

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()

@falkowich falkowich mentioned this issue Jan 1, 2022
8 tasks
prkprime added a commit to prkprime/mooney that referenced this issue Jan 2, 2022
…messages

fortunately nothing broke except for a warning tiangolo/sqlmodel#189

Signed-off-by: Pratik Gorade <me@prkprime.dev>
@curtwagner1984
Copy link

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()

@Gladrat
Copy link

Gladrat commented Jan 4, 2022

I haven't found any way to disable caching for now.
And the same issue occurs even when not using col.

@Data-Zen
Copy link

Data-Zen commented Jan 6, 2022

I am having the same issue. Fix needed ASAP.

@angel-langdon
Copy link

Same issue

@cazdlt
Copy link

cazdlt commented Jan 10, 2022

Same here :)

@Tombrilluin
Copy link

Same issue...

@maresb
Copy link

maresb commented Jan 13, 2022

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.

@mmlynarik
Copy link

mmlynarik commented Jan 14, 2022

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...

@mmlynarik
Copy link

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.

@SamEdwardes
Copy link

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.*")

@memark
Copy link

memark commented Jan 26, 2022

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.

@leynier
Copy link
Contributor

leynier commented Jan 30, 2022

The solution I found was that the inherit_cache field should be set in the Select and SelectOfScalar classes of sqlmodel.sql.expression.

image

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

@rabinadk1
Copy link
Contributor

Just a friendly ping to @tiangolo.

@simbadmorehod
Copy link

simbadmorehod commented Mar 12, 2022

The solution I found was that the inherit_cache field should be set in the Select and SelectOfScalar classes of sqlmodel.sql.expression.

image

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

Mropat added a commit to Clinical-Genomics/genotype-api that referenced this issue Mar 22, 2022
### Fixed
* Workaround for SAWarcning in SQLModel (discussed here: tiangolo/sqlmodel#189)
* Handle missing status detail when analysis is not paired
@linpan
Copy link

linpan commented May 5, 2022

I've solved mine by importing select() from sqlalchemy directly.

have effect side, [{'a': {}}]

hutschen added a commit to hutschen/mv-tool-api that referenced this issue Jul 30, 2022
jmarcet added a commit to jmarcet/sqlmodel that referenced this issue Aug 2, 2022
See: tiangolo#189

Signed-off-by: Javier Marcet <javier.marcet@bigblueanalytics.com>
@tiangolo
Copy link
Owner

Thanks for the report! This was solved in #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! 🍰

@tiangolo tiangolo reopened this Aug 27, 2022
@tiangolo tiangolo added bug Something isn't working answered and removed question Further information is requested labels Aug 27, 2022
@JonasR
Copy link

JonasR commented Aug 28, 2022

I just upgraded to SQLModel 0.0.7 (and SQLAlchemy 1.4.40) and still get this warning. Am I the only one?

@github-actions github-actions bot removed the answered label Aug 28, 2022
@memark
Copy link

memark commented Aug 28, 2022

Yeah, this is not working for me either. (Maybe that's why the bug was reopened?)

image

image

@JonasR
Copy link

JonasR commented Aug 28, 2022

It seems the fix was only applied to expression.py.jinja2, not expression.py
Not sure if that was intentional.

@rabinadk1
Copy link
Contributor

It seems the fix was only applied to expression.py.jinja2, not expression.py Not 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

@JonasR
Copy link

JonasR commented Aug 28, 2022

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.

@tiangolo
Copy link
Owner

Ah! Thanks everyone, good point @rabinadk1, I fixed it in #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. 🚀

chuangbo added a commit to PaiGramTeam/PaiGram that referenced this issue Sep 2, 2022
@nickatnight
Copy link

Can this be closed?

@github-actions github-actions bot removed the answered label Sep 3, 2022
@memark
Copy link

memark commented Sep 3, 2022

@nickatnight Yes, this is now working as expected. The warning is no longer shown.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment