Bulk insert with SQLAlchemy ORM

前端 未结 10 1118
刺人心
刺人心 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:17

    I usually do it using add_all.

    from app import session
    from models import User
    
    objects = [User(name="u1"), User(name="u2"), User(name="u3")]
    session.add_all(objects)
    session.commit()
    

提交回复
热议问题