I have a sqlalchemy relationship like this (trimmed for simplicity):
class Parent(Base):
__tablename__ = 'Parent'
name = Column(String, nullable=False)
def __init__(self, name)
self.name = name
class Child(Base):
__tablename__ = 'Child'
name = Column(String, nullable=False)
parent = relationship(Parent, backref=backref('children')
def __init__(self, name, parent)
self.name = name
self.parent = parent
While working while my objects i do:
parent = Parent("my parent")
db_session.add(parent) # must be done for other reasons not relevant to the issue.
child = Child("my child", parent)
So far so good. But prior to committing when I do the following I get a DB flush:
children = parent.children # using the backref causes a flush
Can this be avoided by changing how I define the backref/relationship?
Using the Session.no_autoflush context manager should achieve what you want in a safe manner:
with session.no_autoflush:
parent = Parent("my parent")
db_session.add(parent)
child = Child("my child", parent)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With