sqlmodel icon indicating copy to clipboard operation
sqlmodel copied to clipboard

SAWarning from sqlalchemy

Open poupryc opened this issue 3 years ago • 28 comments

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(

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 https://github.com/tiangolo/sqlmodel/issues/129#issuecomment-991074972

poupryc avatar Dec 13 '21 18:12 poupryc

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

postpersonality avatar Dec 17 '21 11:12 postpersonality

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.

BurnySc2 avatar Dec 19 '21 12:12 BurnySc2

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 ?

mfrey777 avatar Dec 22 '21 08:12 mfrey777

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

chriswhite199 avatar Dec 27 '21 19:12 chriswhite199

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

curtwagner1984 avatar Jan 04 '22 16:01 curtwagner1984

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

Gladrat avatar Jan 04 '22 16:01 Gladrat

I am having the same issue. Fix needed ASAP.

Data-Zen avatar Jan 06 '22 13:01 Data-Zen

Same issue

angel-langdon avatar Jan 07 '22 08:01 angel-langdon

Same here :)

cazdlt avatar Jan 10 '22 02:01 cazdlt

Same issue...

Tombrilluin avatar Jan 11 '22 17:01 Tombrilluin

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.

maresb avatar Jan 13 '22 07:01 maresb

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 avatar Jan 14 '22 21:01 mmlynarik

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.

mmlynarik avatar Jan 14 '22 22:01 mmlynarik

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

SamEdwardes avatar Jan 20 '22 23:01 SamEdwardes

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.

memark avatar Jan 26 '22 17:01 memark

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

leynier avatar Jan 30 '22 17:01 leynier

Just a friendly ping to @tiangolo.

rabinadk1 avatar Feb 04 '22 05:02 rabinadk1

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

simbadmorehod avatar Mar 12 '22 02:03 simbadmorehod

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

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

linpan avatar May 05 '22 02:05 linpan

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:

tiangolo avatar Aug 27 '22 23:08 tiangolo

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

JonasR avatar Aug 28 '22 07:08 JonasR

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

image image

memark avatar Aug 28 '22 08:08 memark

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

JonasR avatar Aug 28 '22 09:08 JonasR

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

rabinadk1 avatar Aug 28 '22 17:08 rabinadk1

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.

JonasR avatar Aug 28 '22 17:08 JonasR

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:

tiangolo avatar Aug 29 '22 09:08 tiangolo

Can this be closed?

nickatnight avatar Sep 03 '22 08:09 nickatnight

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

memark avatar Sep 03 '22 12:09 memark