Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLAlchemy add vs add_all

I'm trying to figure out when to use session.add and when to use session.add_all with SQLAlchemy.

Specifically, I don't understand the downsides of using add_all. It can do everything that add can do, so why not just always use it? There is no mention of this in the SQLalchemy documentation.

like image 839
Matt Avatar asked Apr 27 '26 05:04

Matt


1 Answers

If you only have one new record to add, then use sqlalchemy.orm.session.Session.add() but if you have multiple records then use sqlalchemy.orm.session.Session.add_all(). There's not really a significant difference, except the API of the first method is for a single instance whereas the second is for multiple instances. Is that a big difference? No. It's just convenience.

like image 94
Donal Avatar answered Apr 28 '26 17:04

Donal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!