Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLAlchemy - INSERT OR REPLACE equivalent

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

Many thanks -- honzas

like image 573
honzas Avatar asked Apr 02 '09 08:04

honzas


2 Answers

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

like image 165
Hadrien Avatar answered Oct 10 '22 00:10

Hadrien


Session.save_or_update(model)
like image 6
M. Utku ALTINKAYA Avatar answered Oct 10 '22 00:10

M. Utku ALTINKAYA