seems to work in SVN trunk, have you tried there ?
On Dec 14, 2007, at 3:27 AM, David J. Mellor wrote:
> from sqlalchemy import *
> from sqlalchemy.orm import *
> from sqlalchemy.orm.interfaces import MapperExtension
>
> class Parent(object):
> pass
>
> class Child(object):
> pass
>
> meta = ThreadLocalMetaData()
> Session = sessionmaker(autoflush=True, transactional=True)
> session = None
>
> class ThreadLocalSession(MapperExtension):
> def get_session(self):
> return session
>
> parents_table = Table(
> "parents", meta,
> Column("id", Integer, Sequence("parents_id_seq"),
> primary_key=True),
> Column("name", String(255), nullable=False))
>
> mapper(Parent, parents_table)
>
> children_table = Table(
> "children", meta,
> Column("id", Integer, Sequence("children_id_seq"),
> primary_key=True),
> Column("name", String(255), nullable=False),
> Column("parent_id", Integer, ForeignKey("parents.id"),
> nullable=False))
>
> # The mapper extension is necessary, otherwise the dynamic query
> returned by
> # Parent.children will not be associated with a session.
> mapper(Child, children_table,
> properties=dict(
> parent=relation(Parent, backref=backref("children",
> lazy="dynamic"))),
> extension=ThreadLocalSession())
>
> engine = create_engine("sqlite:///:memory:", echo=False)
> meta.bind = engine
> session = Session(bind=engine)
> parents_table.create()
> for name in ["parent1", "parent2"]:
> parent = Parent()
> parent.name = name
> session.save(parent)
>
> session.flush()
>
> firstParent = session.query(Parent).get(1)
> secondParent = session.query(Parent).get(2)
> children_table.create()
> for name in ["child1", "child2", "child3", "child4"]:
> child = Child()
> child.name = name
> if name == "child1":
> child.parent = firstParent
> else:
> child.parent = secondParent
>
> session.save(child)
>
> session.flush()
> session.commit()
> session.clear()
>
> first_parent = session.query(Parent).get(1)
> second_parent = session.query(Parent).get(2)
> num_first_children = first_parent.children.count()
> num_second_children = second_parent.children.count()
> assert num_first_children == 1 and num_second_children == 3, \
> "Children in first parent = %s, in second parent = %s" % \
> (num_first_children, num_second_children)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---