I
I
isergeevich6662020-11-01 16:24:49
SQLite
isergeevich666, 2020-11-01 16:24:49

How to delete multiple rows from a table?

Hello!
The table has several columns, one of them with a date like: 2020-09-27T17:00:00.000Z.
It is required to delete several rows with old data for previous days. I make a query like:

delete = DayData.query.filter(DayData.date.between('2020-09-27T16:00:00.000Z', '2020-09-28T11:00:00.000Z'))
db.session.delete(delete)
db.session.commit()


Returns an error:
sqlalchemy.orm.exc.UnmappedInstanceError: Class 'flask_sqlalchemy.BaseQuery' is not mapped


At the same time, if instead of .filter() you use .first(), then it correctly removes the first row from the table.
Help me please

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
isergeevich666, 2020-11-02
@isergeevich666

I found a solution, it was necessary to skip through the cycle.
This code is working:

delete = DayData.query.filter(DayData.date.between('2020-09-27T16:00:00.000Z', '2020-09-28T11:00:00.000Z'))
for i in delete:
    db.session.delete(i)
db.session.commit()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question