Bulk saving complex objects SQLAlchemy

前端 未结 2 733
青春惊慌失措
青春惊慌失措 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()
    
    0 讨论(0)
  • 2020-12-11 02:00

    You can assign ids manually:

    1. Get a write lock on the table

    2. Find the highest existing id

    3. Manually generate an increasing sequence of ids

    Instead of locking the table, you might be able to increment the id sequence in the database to "reserve" a block of ids.

    You'll have to insert in the proper order to avoid foreign key violations (or defer the constraints if your engine allows this).

    0 讨论(0)
提交回复
热议问题