foobar8675
foobar8675

Reputation: 1315

do not know how to remove sqlalchemy 1.4 warning about cache_ok

I am using sqlalchemy 1.4.17 with postgres and have a pytest-asyncio test which calls a function that creates a record that contains a uuid.


async def create_user(session: AsyncSession, input_user_data):
     new_user = model.User(**dict(input_user_data))
     session.add(new_user)
     await session.commit()
class User(Base):
    __tablename__ = "user"
    id = Column(GUID, primary_key=True,
                server_default=DefaultClause(text("gen_random_uuid()")))

and it runs ok, but creates a warning

  sys:1: SAWarning: TypeDecorator GUID() will not produce a cache key because the ``cache_ok`` flag is not set to True.  Set this flag to True if this type object's state is safe to use in a cache key, or False to disable this warning.

and don't understand how to silence it. any help is appreciated!

Upvotes: 2

Views: 4813

Answers (2)

mati.o
mati.o

Reputation: 1768

If for whatever reason you use a custom TypeDecorator, for example:

class MyUUIDType(TypeDecorator):

    impl = sqlalchemy.dialects.postgresql.UUID
    
    ...

Then you'll need to add cache_ok = True as a class member

class MyUUIDType(TypeDecorator):

    impl = sqlalchemy.dialects.postgresql.UUID
    cache_ok = True    

    ...

Surely do this only if your column's TypeDecorator is cacheable.

Upvotes: 2

foobar8675
foobar8675

Reputation: 1315

thanks @snakecharmerb. that pointed me to what I was doing wrong. in case this helps anyone else, I was importing GUID from fastapi_utils and instead imported right from sqlalchemy

# from fastapi_utils.guid_type import GUID, GUID_SERVER_DEFAULT_POSTGRESQL
from sqlalchemy.dialects.postgresql import UUID
class User(Base):
    __tablename__ = "user"
    id = Column(UUID, primary_key=True,
                server_default=DefaultClause(text("gen_random_uuid()")))

rather than change the fastapi utils lib, using the SQL alchemy typedecorator was much easier.

Upvotes: 3

Related Questions