Bulk insert with SQLAlchemy ORM

前端 未结 10 1136
刺人心
刺人心 2020-11-30 17:15

Is there any way to get SQLAlchemy to do a bulk insert rather than inserting each individual object. i.e.,

doing:

INSERT INTO `foo` (`bar`) VALUES (1         


        
10条回答
  •  余生分开走
    2020-11-30 17:55

    This is a way:

    values = [1, 2, 3]
    Foo.__table__.insert().execute([{'bar': x} for x in values])
    

    This will insert like this:

    INSERT INTO `foo` (`bar`) VALUES (1), (2), (3)
    

    Reference: The SQLAlchemy FAQ includes benchmarks for various commit methods.

提交回复
热议问题