0

I am usinf flask,sqlalchemy and sqlite3 for my web application. When I try to the delete operation in my table through UI I am getting an error

delete_user = Usertimeslots.query.filter(Usertimelots.user_id==user_id).all()
print("i am delete",delete_user,file=sys.stderr)
db.session.delete(delete_user)
db.session.commit()

I am trying to delete the row where user id is same in usertimeslots tableuser id

How do I delete the row of the table through flask sqlalchemy

Ilja Everilä
  • 50,538
  • 7
  • 126
  • 127
Bala
  • 21
  • 6

1 Answers1

1

Query.all() returns a list, but Session.delete() expects a model instance. You'll have to apply delete on each object of the list separately in order to mark them for deletion:

for du in delete_user:
    db.session.delete(du)

db.session.commit()

or delete them all with a single bulk operation:

Usertimeslots.query.filter(Usertimelots.user_id==user_id).delete(synchronize_session=False)
db.session.commit()

If on the other hand you expect the query to match a single row only, use Query.one():

delete_user = Usertimeslots.query.filter(Usertimelots.user_id==user_id).one()
db.session.delete(delete_user)
db.session.commit()
Ilja Everilä
  • 50,538
  • 7
  • 126
  • 127