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.

Reply via email to