lazy="raise" on a many to one is problematic because a lot of many-to-one 
operations involve pulling up the object from the identity map, and that's it. 
since people are usually only trying to guard against SQL being emitted, use 
the raise_on_sql option instead:

parent = relationship(Parent, lazy='raise_on_sql')

thanks for the perfect test case BTW makes this very easy for me

On Fri, Nov 22, 2019, at 6:12 AM, Marat Sharafutdinov wrote:
> from sqlalchemy import Column, ForeignKey, Integer, create_engine
> from sqlalchemy.ext.declarative import declarative_base
> from sqlalchemy.orm import relationship, sessionmaker
> 
> Base = declarative_base()
> 
> class Parent(Base):
>  __tablename__ = 'parents'
>  id = Column(Integer, primary_key=True)
> 
> class Child(Base):
>  __tablename__ = 'children'
>  id = Column(Integer, primary_key=True)
>  parent_id = Column(Integer, ForeignKey(Parent.id))
>  parent = relationship(Parent, lazy='raise', passive_deletes=True)
> 
> engine = create_engine('sqlite:///:memory:', echo=True)
> Base.metadata.create_all(engine)
> Session = sessionmaker(bind=engine)
> session = Session()
> 
> # Add
> parent = Parent()
> session.add(parent)
> session.flush()
> child = Child(parent_id=parent.id)
> session.add(child)
> session.commit()
> 
> # Delete
> child = session.query(Child).first()
> session.delete(child)
> session.commit()
> 
> Now I'm getting the following warning:
> /.venv/lib/python3.8/site-packages/sqlalchemy/orm/relationships.py:2021: 
> SAWarning: On Child.parent, 'passive_deletes' is normally configured on 
> one-to-many, one-to-one, many-to-many relationships only.
> 
> If I change `parent` relationship as follows:
> parent = relationship(Parent, lazy='raise', backref=backref('children', 
> passive_deletes=True))
> then I would get the following exception:
> sqlalchemy.exc.InvalidRequestError: 'Child.parent' is not available due to 
> lazy='raise'
> 
> How should it be made properly?
> 

> --
>  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 sqlalchemy+unsubscr...@googlegroups.com.
>  To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sqlalchemy/40f63feb-383e-4fee-8db6-21a757887c54%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/sqlalchemy/40f63feb-383e-4fee-8db6-21a757887c54%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 sqlalchemy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/79e8c08e-c761-4d48-96d8-4cabf6de36de%40www.fastmail.com.

Reply via email to