you would need to accomplish this manually.

existing_obj = session.query(MyClass).get(obj.id)
if existing_obj is not None:
    existing_obj.links.extend(obj.links)
obj = session.merge(obj)



On Sat, Sep 12, 2020, at 3:45 AM, sonsshh wrote:
> Hi,
> 
> I'm trying to change the behavior of Postgres ARRAY type when calling
> 
>     *session.merge(obj)*
> 
> where *obj* has a *links *ARRAY(Text) column. 
> 
> What I want is as below:
>     *obj = Obj(links=['link2'],...)*
> *    in DB I have obj row already and obj.links=['link1']*
> *    session.merge(obj)*
> *    session.commit() updates obj.links=['link1', 'link2'] instead of 
> overwriting obj.links=['link2']*
>     
> I want this sql to be emitted when updating this field.
> 
> *   CASE WHEN obj.links IS NULL *
> *       OR NOT ARRAY[**obj.links**] @> %(links)s
>    THEN array_cat(**obj.links**, %(links)s)
>    ELSE **obj.links** END*
> 
> I've been researching but still got no clue where to start.
> Is there anyone who can help?
> 
> Thanks a lot,
> 
> 

> --
> 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/860da96c-7295-40bf-a436-027e27c1d6d6n%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/sqlalchemy/860da96c-7295-40bf-a436-027e27c1d6d6n%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 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/154a2195-09ef-423d-b74e-394379e9e726%40www.fastmail.com.

Reply via email to