Bulk insert with SQLAlchemy ORM

前端 未结 10 1135
刺人心
刺人心 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 18:13

    SQLAlchemy introduced that in version 1.0.0:

    Bulk operations - SQLAlchemy docs

    With these operations, you can now do bulk inserts or updates!

    For instance, you can do:

    s = Session()
    objects = [
        User(name="u1"),
        User(name="u2"),
        User(name="u3")
    ]
    s.bulk_save_objects(objects)
    s.commit()
    

    Here, a bulk insert will be made.

提交回复
热议问题