> On Feb 27, 2015, at 4:48 PM, Eric Smith <e...@esmithy.net> wrote:
> 
> To those more experienced than me, does this behavior make sense? If so, 
> could you further my education with an explanation?
> 
> If I change an attribute, is_modified returns True (as expected).
> If I then reference a relationship, is_modified returns False (unexpected).
> 
> My output for the following program (using SQLAlchemy 0.9.7) is:
> 
> False
> True
> False
> 
> Thanks,
> Eric
> 
> 
> from sqlalchemy import Column, Integer, String, create_engine, ForeignKey
> from sqlalchemy.ext.declarative import declarative_base
> from sqlalchemy.orm import sessionmaker, relationship
> 
> 
> Base = declarative_base()
> 
> 
> class Parent(Base):
>     __tablename__ = 'parent'
>     id = Column(Integer, primary_key=True, nullable=False)
>     message = Column(String)
>     children = relationship("Child", backref='parent')
> 
> 
> class Child(Base):
>     __tablename__ = 'child'
>     id = Column(Integer, primary_key=True, nullable=False)
>     parent_id = Column(Integer, ForeignKey('parent.id'))
> 
> 
> def main():
>     engine = create_engine('sqlite:///:memory:')
>     Session = sessionmaker(bind=engine)
>     Base.metadata.create_all(engine)
> 
>     db = Session()
>     parent = Parent()
>     db.add(parent)
>     db.commit()
> 
>     print db.is_modified(parent)
>     parent.message = "hi"
>     print db.is_modified(parent)
>     len(parent.children)  # Should this change is_modified?
>     print db.is_modified(parent)

Turn on sql echoing, which is always the first step towards understanding ORM 
behavior, and you'll see that the call to parent.children invokes a lazy load 
operation, which is a Query hence it autoflushes first, thereby synchronizing 
the state of "message" and resetting the modified flag. 



> 
> if __name__ == '__main__':
>     main()
> -- 
> 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 sqlalchemy+unsubscr...@googlegroups.com.
> To post to this group, send email to sqlalchemy@googlegroups.com.
> Visit this group at http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

-- 
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 sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to