Search code examples
pythonsqlalchemy

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.


Solution

  • 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.