sqlalchemy easy way to insert or update?

前端 未结 1 560
一整个雨季
一整个雨季 2021-02-02 06:39

I have a sequence of new objects. They all look like similar to this:

Foo(pk_col1=x, pk_col2=y, val=\'bar\')

Some of those are Foo that exist (i.e. only val diff

相关标签:
1条回答
  • 2021-02-02 07:14

    I think you are after new_obj = session.merge(obj). This will merge an object in a detached state into the session if the primary keys match and will make a new one otherwise. So session.save(new_obj) will work for both insert and update.

    0 讨论(0)
提交回复
热议问题