most (although some are missing) of the examples illustrate it such as 
https://docs.sqlalchemy.org/en/14/core/custom_types.html#store-timezone-aware-timestamps-as-timezone-naive-utc

class TZDateTime(TypeDecorator):
    impl = DateTime
    cache_ok = True

    def process_bind_param(self, value, dialect):
        if value is not None:
            if not value.tzinfo:
                raise TypeError("tzinfo is required")
            value = value.astimezone(datetime.timezone.utc).replace(
                tzinfo=None
            )
        return value

    def process_result_value(self, value, dialect):
        if value is not None:
            value = value.replace(tzinfo=datetime.timezone.utc)
        return value


On Wed, May 26, 2021, at 5:38 PM, Jinghui Niu wrote:
> Thank you. Could you please also give some hint on a case where we must set 
> this attribute to `False`?
> 
> On Wed, May 26, 2021 at 5:54 AM Mike Bayer <mike...@zzzcomputing.com> wrote:
>> __
>> 
>> 
>> On Wed, May 26, 2021, at 3:07 AM, niuji...@gmail.com 
>> <mailto:niuji...%40gmail.com> wrote:
>>> I have consistently receiving the warning: 
>>> 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.
>>> 
>>> 
>>> After reading the documentation, I learned that the "cache_ok" class-level 
>>> attribute can be set to either True or False. But the documentation is very 
>>> abstract on when to use it? What is cache key? Is it for loading and 
>>> caching a set of objects from the database? Or caching the query itself?
>> 
>> the caching system is used to cache the generated SQL and related parameter 
>> and result-handling structures for a statement object and is described at:
>> 
>> https://docs.sqlalchemy.org/en/14/core/connections.html#sql-compilation-caching
>> 
>> it is always safe to set cache_ok=False on your TypeDecorator class,  you 
>> will however lose the benefit of SQL statement caching for statements which 
>> include use of this datatype.
>> 
>> 
>>> 
>>> If my TypeDecorator class doesn't even have a __init__method, just 
>>> "process_bind_param" and "process_result_value" two methods, do I even need 
>>> to bother with this "cache_ok" setting at all?
>> 
>> If your TypeDecorator is stateless, then it's fine to set cache_ok=True.
>> 
>> 
>>> 

>>> -- 
>>> SQLAlchemy - 
>>> The Python SQL Toolkit and Object Relational Mapper
>>>  
>>> http://www.sqlalchemy.org/
>>>  
>>> To post example code, please provide an MCVE: Minimal, Complete, and 
>>> Verifiable Example. See http://stackoverflow.com/help/mcve for a full 
>>> description.
>>> --- 
>>> You received this message because you are subscribed to the Google Groups 
>>> "sqlalchemy" group.
>>> To unsubscribe from this group and stop receiving emails from it, send an 
>>> email to sqlalchemy+unsubscr...@googlegroups.com.
>>> To view this discussion on the web visit 
>>> https://groups.google.com/d/msgid/sqlalchemy/abe578f0-fd11-468f-857d-dee6fd77ebc5n%40googlegroups.com
>>>  
>>> <https://groups.google.com/d/msgid/sqlalchemy/abe578f0-fd11-468f-857d-dee6fd77ebc5n%40googlegroups.com?utm_medium=email&utm_source=footer>.
>> 
>> 

>> -- 
>> SQLAlchemy - 
>> The Python SQL Toolkit and Object Relational Mapper
>>  
>> http://www.sqlalchemy.org/
>>  
>> To post example code, please provide an MCVE: Minimal, Complete, and 
>> Verifiable Example. See http://stackoverflow.com/help/mcve for a full 
>> description.
>> --- 
>> You received this message because you are subscribed to a topic in the 
>> Google Groups "sqlalchemy" group.
>> To unsubscribe from this topic, visit 
>> https://groups.google.com/d/topic/sqlalchemy/zCPZKTxM6b0/unsubscribe.
>> To unsubscribe from this group and all its topics, send an email to 
>> sqlalchemy+unsubscr...@googlegroups.com.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/sqlalchemy/f3640072-8d16-4f59-bc67-f468c78878cb%40www.fastmail.com
>>  
>> <https://groups.google.com/d/msgid/sqlalchemy/f3640072-8d16-4f59-bc67-f468c78878cb%40www.fastmail.com?utm_medium=email&utm_source=footer>.
> 

> -- 
> SQLAlchemy - 
> The Python SQL Toolkit and Object Relational Mapper
>  
> http://www.sqlalchemy.org/
>  
> To post example code, please provide an MCVE: Minimal, Complete, and 
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full 
> description.
> --- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to sqlalchemy+unsubscr...@googlegroups.com.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sqlalchemy/CAOQAhNf94F7XHBZ28LEKe5TmC7Q4vKRbiNSZX48waASnGgYJPQ%40mail.gmail.com
>  
> <https://groups.google.com/d/msgid/sqlalchemy/CAOQAhNf94F7XHBZ28LEKe5TmC7Q4vKRbiNSZX48waASnGgYJPQ%40mail.gmail.com?utm_medium=email&utm_source=footer>.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/2b5556e9-123b-42ad-8a01-7039c05f8b55%40www.fastmail.com.

Reply via email to