On Mon, May 13, 2019 at 4:18 PM Tony Cao <t...@benchling.com> wrote:
>
> Hi all,
>
> Is there way to use Column.onupdate conditionally? For example, say I have:
>
> class A(Base):
>     foo = Column(String)
>     bar = Column(String)
>     foo_updated = Column(DateTime, onupdate=update_fn) # Should only update 
> when foo is updated
>
> def update_fn(context):
>     if ...: # How can I check if only foo was updated?
>         return datetime.now()
>     else:
>         return ... # How can I say to not update?
>
> Is there a way to define update_fn to only update foo_updated when foo 
> changes? I can look at context.get_current_parameters() to see what columns 
> are being used in the compiled statement, but it doesn't explicitly say which 
> columns are the ones actually being updated; for example, if I have
>
> A.query.filter(A.bar == 'test').update({A.foo: 'new'}, 
> synchronize_session=False)
>
> then context.get_current_parameters will return a dict with keys for both 
> 'bar' and 'foo', although it looks like it suffixes the filter param with a 
> '_1' - is that something I can rely on to know if a column is used as a 
> filter instead of an update? And beyond that, is there a way I can specify to 
> not update the column?
>
> Alternatively, is there another approach recommended to doing this?

the most common way is to use the ORM level before_update event:

https://docs.sqlalchemy.org/en/13/orm/events.html?highlight=before_update#sqlalchemy.orm.events.MapperEvents.before_update

there's also before_execute() at the Core level, but before the UPDATE
statement is even written, e.g. at the ORM level, is the best way.
Once you are in the onupdate Python function, you have to return a
value, as this occurs well after the UPDATE statement has been
compiled.




>
> Thanks!
>
> --
> 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 post to this group, send email to sqlalchemy@googlegroups.com.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sqlalchemy/b66e89c9-6618-4600-9381-182fa101f5b6%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

-- 
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 post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/CA%2BRjkXHV-vQUeSpMGQdX7qZCYMZyfVU8FNuj1t4iPXJuPhX-cw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to