Say I have a model instance like this:
instance = session.query(MyModel).filter_by(id=1).first()
How can I delete that row? Is there a special method to call?
Say I have a model instance like this:
instance = session.query(MyModel).filter_by(id=1).first()
How can I delete that row? Is there a special method to call?
Ok I found it after further searching:
session.delete(instance)
session.delete(instance)
rather than session.query(InstanceModel).filter_by(a=instance.a, ...).delete()
(typical relation model). –
Nauseate You can fire a Single query for this.
For all records:
session.query(MyModel).delete()
session.commit()
It will delete all records from it and if you want to delete specific records then try filter clause in the query. ex.
For specific value:
session.query(MyModel).filter(MyModel.id==1).delete()
session.commit()
sqlalchemy.exc.InvalidRequestError: SQL expression, column, or mapped entity expected - got '<class '__main__.ModelName'>'
. –
Annulet For me, it's working with delete and commit:
session.delete(instance)
session.commit()
© 2022 - 2025 — McMap. All rights reserved.