what you are seeing is somewhat backwards due to what might be a bug. in the 
example you have, *both* calls to group.users should be raising, because this 
object is already present in the identity map so your lazyload() option should 
not affect this already-present Group object.

if you load the Group newly into a new Session, or session.close() the one you 
have, then the lazyload() option is associated with the Group and it will lazy 
load every time for the lifespan of that object.

I'm not sure yet why the lazyload() is somehow taking effect for the first 
group.users in your example, though. it's not supposed to.



On Wed, Sep 25, 2019, at 1:07 PM, Marat Sharafutdinov wrote:
> from sqlalchemy import Column, ForeignKey, Integer, create_engine
> from sqlalchemy.ext.declarative import declarative_base
> from sqlalchemy.orm import lazyload, relationship, sessionmaker
> 
> Base = declarative_base()
> 
> 
> class Group(Base):
>  __tablename__ = 'groups'
> 
>  id = Column(Integer, primary_key=True)
> 
>  users = relationship('User', lazy='raise')
> 
> 
> class User(Base):
>  __tablename__ = 'users'
> 
>  id = Column(Integer, primary_key=True)
>  group_id = Column(Integer, ForeignKey('groups.id'))
> 
> 
> engine = create_engine('sqlite:///:memory:', echo=True)
> Base.metadata.create_all(engine)
> 
> Session = sessionmaker(bind=engine)
> session = Session()
> 
> group = Group()
> session.add(group)
> session.commit()
> 
> group = session.query(Group).options(lazyload(Group.users)).first()
> print('SUCCESS', group.users)
> session.refresh(group)
> print('FAILURE', group.users)
> 
> Is `session.expire(group)` is the only way to reload collections and related 
> items?
> 

> --
>  SQLAlchemy - 
>  The Python SQL Toolkit and Object Relational Mapper
> 
> http://www.sqlalchemy.org/
> 
>  To post example code, please provide an MCVE: Minimal, Complete, and 
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full 
> description.
>  --- 
>  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 view this discussion on the web visit 
> https://groups.google.com/d/msgid/sqlalchemy/43ce8eb9-367d-43da-a371-0fa5e157e0de%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/sqlalchemy/43ce8eb9-367d-43da-a371-0fa5e157e0de%40googlegroups.com?utm_medium=email&utm_source=footer>.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/d8864771-f2ec-48d4-a5fe-e7798926666e%40www.fastmail.com.

Reply via email to