我有一个名为data_column的表,以便以下代码可用于选择data_column.dc_name等于字符串myNewName的第一行:
data_column.query.filter_by(dc_name=myNewName).first()
如果我想执行等效删除(delete from data_column where dc_name = myNewName
),我怎么能实现呢?
以下代码无效:
data_column.query.filter_by(dc_name=myNewName).delete() #the delete appears to be ignored
data_column.query.delete().filter_by(dc_name=myNewName) #this tries to delete everything in the table, the filter appears to get ignored
答案 0 :(得分:1)
由于您使用的是flask-sqlalchemy,因此需要从db会话中删除它并提交。下面的代码假定'db'是您的sqlalchemy会话对象。
my_row = data_column.query.filter_by(dc_name=myNewName).first()
# delete the row from db session if it exists
if my_row is not None:
db.session.delete(my_row)
db.session.commit()