SQLalchemy Bulk insert with one to one relation

后端 未结 1 1041
無奈伤痛
無奈伤痛 2021-01-15 21:22

I have the following model where TableA and TableB have 1 to 1 relationship:

class TableA(db.Model):
    id = Column(db.BigInteger, primary_key=True)
    tit         


        
相关标签:
1条回答
  • 2021-01-15 22:12

    The error you see is thrown by Mysql. It is complaining that the attempt to insert records into table_b violates the foreign key constraint.

    One technique could be to write all the titles in one bulk statement, then write all the names in a 2nd bulk statement. Also, I've never passed relationships successfully to bulk operations, to this method relies on inserting simple values.

    bulk_titles = [TableA(title=title) for title in titles]
    session.bulk_save_objects(bulk_titles, return_defauls=True)
    bulk_names = [TableB(id=title.id, name=name) for title, name in zip(bulk_titles, names)]
    session.bulk_save_objects(bulk_names)
    

    return_defaults=True is needed above because we need title.id in the 2nd bulk operation. But this greatly reduces the performance gains of the bulk operation

    To avoid the performance degradation due to return_defauts=True, you could generate the primary keys from the application, rather than the database, e.g. using uuids, or fetching the max id in each table and generating a range from that start value.

    Another technique might be to write your bulk insert statement using sqlalchemy core or plain text.

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