Forums

SQLAlchemy update doesn't work. Any suggestions?

I want the code that does the following procedure: UPDATE comments SET content=contents WHERE id=id; This changes the content and preserves its id assignment. So the id is now paired with the new content. SQLalchemy claims this can be accomplished as follows: 1. update = Comment.query.filter_by( id=id).first() or update = Comment.query.get(id); 2. update.content = contents; 3. db.session.commit (some methods include a db.session.add(update)). I have tried many versions, but some delete the original content and add the new content, or, keep the original content with its original id and add the new content at the end of the database. In any case, a new id is assigned. Is there SQLalchemy code that does a real update or edit? Is there a way to bypass SQLalchemy and pass on a SQL command?

This looks like it does what you want: https://docs.sqlalchemy.org/en/14/core/selectable.html#sqlalchemy.sql.expression.TableClause.update

Thanks! Got. it working!

Excellent!