开发者

SQL Alchemy - How to Delete from a model instance?

开发者 https://www.devze.com 2023-01-08 10:12 出处:网络
Say I get a model instance like this: instance = session.query(MyModel).filter_by(id=1).first() How can I delete that开发者_C百科 row?Is there a special method to call?Ok I found it after further s

Say I get a model instance like this:

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

How can I delete that开发者_C百科 row? Is there a special method to call?


Ok I found it after further searching:

session.delete(instance)


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()
0

精彩评论

暂无评论...
验证码 换一张
取 消