SQLAlchemy - INSERT OR REPLACE equivalent

前端 未结 4 606
再見小時候
再見小時候 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 09:54

    I don't think (correct me if I'm wrong) INSERT OR REPLACE is in any of the SQL standards; it's an SQLite-specific thing. There is MERGE, but that isn't supported by all dialects either. So it's not available in SQLAlchemy's general dialect.

    The cleanest solution is to use Session, as suggested by M. Utku. You could also use SAVEPOINTs to save, try: an insert, except IntegrityError: then rollback and do an update instead. A third solution is to write your INSERT with an OUTER JOIN and a WHERE clause that filters on the rows with nulls.

提交回复
热议问题