On Tue, Aug 25, 2015 at 6:43 PM, Abhishek Sharma <[email protected]> wrote:
> Hi Team, > We are executing select query using > self.session.query(Model).filter(filter_conditions).first() > > Then we are storing the about query result in result variable. > Then we are trying to update one of model attribute like > result.description=value > Even though we have not added that object in session to update the DB and > there is no commit but sqlalchemy emitting update query on DB. > > Objects loaded via session.query(...) are already attached to a session; session.add(...) is for *new* objects. If you modify an object attached to a session, SQLAlchemy will consider it "dirty", and will UPDATE the database when the session is next flushed. By default, SQLAlchemy will autoflush the session whenever you use it to query the database, to synchronise the application state with the database. If it didn't do this, the query could potentially return incorrect results. (Imagine you change an attribute on your object from 1 to 2, then you query for all objects where the attribute equals 1. If SA didn't flush the session first, your modified object would match the query, even though in the application it has changed.) There's some information about that in the docs at http://docs.sqlalchemy.org/en/rel_1_0/orm/session_basics.html#flushing Hope that helps, Simon -- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
