I've seen several answers on StackOverflow saying that this is a way to update a record:
session.query(FoobarModel).get(foobar_id).update({'name': 'New Foobar Name!'})
However, I get an error saying:
AttributeError: 'FoobarModel' object has no attribute 'update'
I was able to update like this, however:
foobar = session.query(FoobarModel).get(foobar_id)
foobar.name = 'New Foobar Name!'
session.commit()
session.flush()
So then I tried something like this (so that I don't have to write out every property):
new_foobar = {'name': 'New Foobar Name!'}
old_foobar = session.query(FoobarModel).get(foobar_id)
for property in new_foobar:
old_foobar[property] = new_foobar[property]
session.commit()
session.flush()
However, I then get this error:
TypeError: 'FoobarModel' object does not support item assignment
After doing some digging I found out that it's because even this wouldn't work:
print(old_foobar['name'])
Which throws the error:
TypeError: 'FoobarModel' object is not subscriptable
Honestly the first syntax would be the best in my opinion, but I can't get it to work. Any ideas?
Note: I am not using Flask-SQLAlchemy here, I'm using SQLAlchemy ORM.