The following code raises a FlushError because the child object named
Cheshire is an orphan and the delete-orphan cascade rule is in effect.
It seems to me that an orphan which has no database identity should be
detached from its session, just as it was attached when appended to a
parent's list of children. Why does SA raise an exception instead?


from sqlalchemy import *

import sys

meta = BoundMetaData(sys.argv[1])

parentTable = Table("tether_parents",
                  meta,
                  Column("id", Integer,
Sequence("seq_tether_parents"), primary_key = True),
                  Column("name", String(10), nullable = False))


childTable = Table("tether_children",
                   meta,
                   Column("id", Integer,
Sequence("seq_tether_children"), primary_key = True),
                   Column("parent_id", Integer,
ForeignKey("tether_parents.id"), nullable = False),
                   Column("name", String(10), nullable = False))

class Child(object):
    def __init__(self, name):
        self.name = name
    def __repr__(self):
        return "Child(%s)" % repr(self.name)

class Parent(object):
    def __init__(self, name):
        self.name = name
        children = []
    def __repr__(self):
        return "Parent(%s)" % repr(self.name)

mapper(Child, childTable)

mapper(Parent, parentTable, properties = dict(children =
relation(Child,cascade="all,delete-orphan")))

meta.drop_all()
meta.create_all()

meta.engine.echo = False

sess = create_session()

p = Parent("Carroll")
p.children = [Child("Alice"), Child("Dee"), Child("Dum")]
sess.save(p)
sess.flush()
sess.close()

sess = create_session(echo_uow = True)
p = sess.query(Parent).get_by_name("Carroll")
# New child.
p.children.append(Child("Cheshire"))
# Oops, changed my mind.
del p.children[3]
sess.flush()
sess.close()


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to