SQL Alchemy - how to remove from a model instance?

Let's say I get an instance of the model as follows:

instance = session.query(MyModel).filter_by(id=1).first() 

How to delete this line? Is there a special method to call?

+7
python orm sqlalchemy
source share
2 answers

Ok, I found it after further searching:

 session.delete(instance) 
+18
source share

You can run one query for this.

for all entries

 db.session.query(Table_name).delete() db.session.commit() 

here db is an object of the Flask SQLAlchemy class. It will delete all entries from it, and if you want to delete specific entries, try filter_by in the query. e.g.

for a specific value

 db.session.query(Table_name).filter_by(id==123).delete() db.session.commit() 
0
source share

All Articles