SQLAlchemy - INSERT OR REPLACE equivalent

前端 未结 4 546
再見小時候
再見小時候 2021-01-04 09:37

does anybody know what is the equivalent to SQL \"INSERT OR REPLACE\" clause in SQLAlchemy and its SQL expression language?

Many thanks -- honzas

4条回答
  •  执念已碎
    2021-01-04 10:14

    What about Session.merge?

    Session.merge(instance, load=True, **kw)
    

    Copy the state an instance onto the persistent instance with the same identifier.

    If there is no persistent instance currently associated with the session, it will be loaded. Return the persistent instance. If the given instance is unsaved, save a copy of and return it as a newly persistent instance. The given instance does not become associated with the session. This operation cascades to associated instances if the association is mapped with cascade="merge".

    from http://www.sqlalchemy.org/docs/reference/orm/sessions.html

提交回复
热议问题