current position:Home>sqlalchemy bulk insert

sqlalchemy bulk insert

2022-08-06 09:24:06Xiaodou Whole Sugar (Cx)

  1. Session object has provided bulk_save_objectsmethod
    But use this method need to pay attention that it may cause memory overflow

  2. Recommended method

@app.post('/multi')async def multi(db:Session=Depends(get_db)):# db.bulk_save_objectsdb.execute(Student.__table__.insert(),[{"name": 'NAME ' + str(i)} for i in range(100)])return "success"
def get_db():db: Session = SessionLocal()try:yield dbdb.commit()except Exception:db.rollback()finally:db.close()

copyright notice
author[Xiaodou Whole Sugar (Cx)],Please bring the original link to reprint, thank you.
https://en.chowdera.com/2022/218/202208060920324861.html

Random recommended