Hello,
I'm curious about the behaviour of foreign keys when modifying collections
as I want to be able to get the value before flush for some business logic
validation.
The following is a cut down version of the association list example:
import uuid
from sqlalchemy import Column, ForeignKey, Integer, String, create_engine
from sqlalchemy.orm import Session, relationship, backref
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
class TreeNode(Base):
__tablename__ = 'tree'
id = Column(String(36), primary_key=True)
parent_id = Column(Integer, ForeignKey(id))
children = relationship(
'TreeNode', backref=backref('parent', remote_side=id)
)
def __init__(self, id=None, *args, **kwargs):
if id is None:
id = str(uuid.uuid4())
self.id = id
super(TreeNode, self).__init__(*args, **kwargs)
if __name__ == '__main__':
engine = create_engine('sqlite://')
Base.metadata.create_all(engine)
session = Session(engine)
node = TreeNode()
session.add(node)
print node.id # some uuid
child_node = TreeNode()
node.children.append(child_node)
# Question: Why is parent_id not set to node.id even though it was
available?
print child_node.parent_id
Note that the child_node.parent_id is None even though the referenced value
was present in the session. I know a flush will fix this, but I want to
check this value is not None for certain 'types' of node before flush if
possible.
Is there a way to configure that behaviour or should I be creating my own
collection listeners to handle this?
cheers,
Martin
--
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 post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.