How to make update in flask-sqlalchemy?

Pure sql query looks like this:
update stat set user_id=1 where user_id=2

Googled this, but all solutions didn't fit.

The user_id entry in the table stat are not unique.
Here's an example:
==========================
MariaDB []> select id , date , user_id from stat where user_id = 2 limit 0,3;
+-------+---------------------+---------+
| id | date | user_id |
+-------+---------------------+---------+
| 63573 | 2011-12-18 17:20:17 | 2 |
| 63653 | 2011-12-18 18:51:33 | 2 |
| 63654 | 2011-12-18 18:51:39 | 2 |
+-------+---------------------+---------+
3 rows in set (0.00 sec)

========================

And all the decisions that gugliada they are designed to update only one row.
September 19th 19 at 12:51
3 answers
September 19th 19 at 12:53
Solution
Using filter or filter_by possible to obtain the necessary string - filter and filter_by. Then in a loop around all the records and record the desired value.

BUT! this is not the best approach, rather it is a bad approach! In SQLAlchemy, there is one good method - update. In the end you can do all this:
u_id = get_userid()
rows = Stat.query.filter_by(user_id = u_id).update({'user_id': 1})
db.session.commit()


In General, no need to Google, everything is there in the official documentation.
Thanks for the reply. But it doesn't work.
Error code:

========================
Traceback (most recent call last):
File "/home/dom/env/lib/python3.4/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/home/dom/env/lib/python3.4/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/dom/env/lib/python3.4/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/dom/env/lib/python3.4/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/home/dom/env/lib/python3.4/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/home/dom/env/lib/python3.4/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/dom/proj/main.py", line 744, in profile_clearstat
db.session.query().filter(S. user_id == user_id).update({"user_id": 1})
File "/home/dom/env/lib/python3.4/site-packages/sqlalchemy/orm/query.py", line 2796, in update
update_op.exec_()
File "/home/dom/env/lib/python3.4/site-packages/sqlalchemy/orm/persistence.py", line 895, in exec_
self._do_pre()
File "/home/dom/env/lib/python3.4/site-packages/sqlalchemy/orm/persistence.py", line 903, in _do_pre
self.context = context = query._compile_context()
File "/home/dom/env/lib/python3.4/site-packages/sqlalchemy/orm/query.py", line 2836, in _compile_context
"Query contains no columns with which to "
sqlalchemy.exc.InvalidRequestError: Query contains no columns with which to SELECT from.
======================================

And the code itself:

user_id = get_userid()
S = Stat
db.session.query().filter(S. user_id == user_id).update({"user_id": 1})
db.session.commit() - Elda.Abbo commented on September 19th 19 at 12:56
My codes above is just an example of how the update method works - first, the condition of the sample, then update to update. On this principle work the majority of the ORM. But it does not mean that it is necessary to copy simply. The error you have here is that you do not set fields for output. In other words, perform a query for a specific table, for example, as follows: Stat.query.filter(user_id == '2').update({"user_id": 1}) - Oren92 commented on September 19th 19 at 12:59
In this case, nothing happens and changes.
And error no prints. - Elda.Abbo commented on September 19th 19 at 13:02
: show code as in the example above you did. - Oren92 commented on September 19th 19 at 13:05
: Yes here is the code:

user_id = get_userid()
Stat.query.filter(Stat.user_id == user_id).update({"user_id": 1})
db.session.commit() - Elda.Abbo commented on September 19th 19 at 13:08
Try this:
u_id = get_userid()
rows = Stat.query.filter_by(user_id = u_id).update({'user_id': 1})
db.session.commit() - Oren92 commented on September 19th 19 at 13:11
Yes, thank you! Worked.
And how much do we need in the end
db.session.commit()?
And what to do with the object rows, if I don't need it? - Elda.Abbo commented on September 19th 19 at 13:14
Commit necessarily need to run the query and to complete the transaction. Rows can not use it in this variable returns the number of records affected. In fact, if this information is not needed, it can be used without rows, i.e. just a Stat.query.filter_by(user_id = u_id).update({'user_id': 1}) - Oren92 commented on September 19th 19 at 13:17
Yes, that row was removed, everything works fine. Thanks again! - Elda.Abbo commented on September 19th 19 at 13:20
September 19th 19 at 12:55
user = User.query.get(1)
user.email = 'ww@gmail.com'
db.session.add(user)
db.session.commit()

blog.miguelgrinberg.com/post/the-flask-mega-tutori...
The problem is that this is not a unique entry.
Well, that is in the table Stat a lot of times
user_id = 2

And here we are talking about changing a single record. - Elda.Abbo commented on September 19th 19 at 12:58
September 19th 19 at 12:57
It is possible to write the migration. If anything goes wrong, rollback.
For flask there is a plugin (which is a wrapper for the alembic)

Find more questions by tags FlaskPython