smoother with_parent() failure modes
Author: zzzeekCreated Aug 6, 2021Updated Sep 12, 2026
Labelsbugormcode review in progress
Describe the bug
see #6859 where we had some confusion and with_parent() should do a better job reporting on failure modes, here are some:
To Reproduce
from sqlalchemy import Column
from sqlalchemy import create_engine
from sqlalchemy import ForeignKey
from sqlalchemy import Integer
from sqlalchemy import String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy.orm import Session
from sqlalchemy.orm import with_parent
Base = declarative_base()
class A(Base):
__tablename__ = 'a'
id = Column(Integer, primary_key=True)
data = Column(String)
bs = relationship("B", backref="a")
class B(Base):
__tablename__ = 'b'
id = Column(Integer, primary_key=True)
a_id = Column(ForeignKey("a.id"))
data = Column(String)
e = create_engine("sqlite://", echo=True)
Base.metadata.create_all(e)
s = Session(e)
s.add(A(bs=[B()]))
s.commit()
a1 = s.query(A).first()
b1 = s.query(B).first()
def test_one():
print(
with_parent(a1, B.a).compile(compile_kwargs={"literal_binds": True})
)
def test_two():
print(
with_parent(b1, B.a).compile(compile_kwargs={"literal_binds": True})
)
def test_three():
print(
with_parent(a1, A.bs).compile(compile_kwargs={"literal_binds": True})
)
def test_four():
print(
with_parent(b1, A.bs).compile(compile_kwargs={"literal_binds": True})
)
# additional TODO: relationhips with backwards foreign(), remote(),
# which don't derive from the object at allError
n/a
Versions
zeek
Additional context
No response
Source: sqlalchemy/sqlalchemy