SQLAlchemy INSERT IGNORE

后端 未结 2 774
走了就别回头了
走了就别回头了 2020-12-14 07:37

How can I insert multiple data records into table ignoring duplicates. I am using SQLAlchemy.

Thank you!

2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-14 07:59

    To always replace INSERT by INSERT OR IGNORE, you can use a compiler extension:

    from sqlalchemy.ext.compiler import compiles
    from sqlalchemy.sql.expression import Insert
    
    @compiles(Insert)
    def _prefix_insert_with_ignore(insert, compiler, **kw):
        return compiler.visit_insert(insert.prefix_with('OR IGNORE'), **kw)
    

    Or to do this only temporarily, call the compiles decorator manually and use deregister once you're done:

    from sqlalchemy.ext.compiler import compiles, deregister
    from sqlalchemy.sql.expression import Insert
    
    def _prefix_insert_with_ignore(insert, compiler, **kw):
        return compiler.visit_insert(insert.prefix_with('OR IGNORE'), **kw)
    
    compiles(Insert)(_prefix_insert_with_replace)
    try:
        # do some inserts...
    finally:
        deregister(Insert)
    

    This does feel hacky because it's still a global change, but as long as you don't use threads and make sure everything is properly committed before the deregister call, it's probably okay.

提交回复
热议问题