开发者

SQL Alchemy - How to Delete from a model instance?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜