How to delete a record by id in Flask-SQLAlchemy

后端 未结 3 1351
我寻月下人不归
我寻月下人不归 2020-12-04 09:41

I have users table in my MySql database. This table has id, name and age fields.

How can I delete some record by

3条回答
  •  眼角桃花
    2020-12-04 10:28

    Just want to share another option:

    # mark two objects to be deleted
    session.delete(obj1)
    session.delete(obj2)
    
    # commit (or flush)
    session.commit()
    

    http://docs.sqlalchemy.org/en/latest/orm/session_basics.html#deleting

    In this example, the following codes shall works fine:

    obj = User.query.filter_by(id=123).one()
    session.delete(obj)
    session.commit()
    

提交回复
热议问题