Question

I have a table called data_column such that the following code works in selecting the first row where data_column.dc_name is equal to a string myNewName:

data_column.query.filter_by(dc_name=myNewName).first()

If I wanted to perform the equivalent delete (delete from data_column where dc_name = myNewName), how could I achieve that?

NONE of the following code works:

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
Was it helpful?

Solution

Since you are using flask-sqlalchemy, you need to delete it from the db session and also commit. The code below assumes 'db' is your sqlalchemy session object.

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()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top