我正在使用postgres的sqlalchemy 1.4.17,并且有一个pytest-asyncio测试,它调用一个函数来创建一个包含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()")))它运行正常,但会产生一个警告
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.却不知道如何让它安静下来。如有任何帮助,我们不胜感激!
发布于 2021-06-07 06:20:19
谢谢@snakecharmerb。这让我意识到我做错了什么。如果这对其他人有帮助,我从fastapi_utils导入GUID,而不是直接从sqlalchemy导入
# from fastapi_utils.guid_type import GUID, GUID_SERVER_DEFAULT_POSTGRESQL
from sqlalchemy.dialects.postgresql import UUIDclass User(Base):
__tablename__ = "user"
id = Column(UUID, primary_key=True,
server_default=DefaultClause(text("gen_random_uuid()")))与更改fastapi utils库相比,使用SQL炼金术类型装饰器要容易得多。
https://stackoverflow.com/questions/67855798
复制相似问题