HomestuckBrother
HomestuckBrother

Reputation: 53

Flask-sqlalchemy Rowproxy cannot be modified

I am trying to update records 'status' from my database where the 'sendtime' is in the past. I am receiving a set of records that I can iterate over and see all their fields with no issue. The issue arises when I attempt to update the status field.

The following results in Rowproxy does not support item assignment:

query = ('SELECT * FROM jobs '
    'WHERE status="INCOMPLETE" '
    'AND sendtime <= NOW()')
overdueJobs = db.engine.execute(query).fetchall()
for overdueJob in overdueJobs:
    overdueJob.status = "COMPLETE"
db.session.commit()

I read this answer on SO based on the error above and while converting to a dict allows me to edit the fields without errors, it does not actually commit anything to the database.

I believe I am doing something wrong regarding how I use the queried results. I think I have to use my jobs model somehow to access the fields, but the documentation and other questions I found on SO are for a single result from a query. What I have can be more than that. How do I edit the fields of each record returned from this query?

Upvotes: 0

Views: 169

Answers (1)

HomestuckBrother
HomestuckBrother

Reputation: 53

I found the answer to my question here.

I needed to modify my query from

overdueJobs = db.engine.execute(query).fetchall()

To

overdueJobs = jobs.query.from_statement(db.text(query)).all()

Where jobs is a model in my database. Then I could access each row as a model by looping over the returned result.

Upvotes: 1

Related Questions