Bulk saving complex objects SQLAlchemy

前端 未结 2 745
青春惊慌失措
青春惊慌失措 2020-12-11 01:34
association_table = Table(\"association_table\",
                          Base.metadata,
                          Column(\"show_id\", Integer(), ForeignKey(\"show_         


        
2条回答
  •  北荒
    北荒 (楼主)
    2020-12-11 01:59

    Session.bulk_save_objects() is too low level API for your use case, which is persisting multiple model objects and their relationships. The documentation is clear on this:

    Warning

    The bulk save feature allows for a lower-latency INSERT/UPDATE of rows at the expense of most other unit-of-work features. Features such as object management, relationship handling, and SQL clause support are silently omitted in favor of raw INSERT/UPDATES of records.

    Please read the list of caveats at Bulk Operations before using this method, and fully test and confirm the functionality of all code developed using these systems.

    You should use Session.add_all() to add a collection of instances to the session. It will handle the instances one at a time, but that is the price you have to pay for advanced features such as relationship handling.

    So, instead of

    session.bulk_save_objects(showtime_lists)
    session.commit()
    

    do

    session.add_all(showtime_lists)
    session.commit()
    

提交回复
热议问题