summaryrefslogtreecommitdiff
path: root/examples/backref/backref_tree.py
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2006-03-07 03:14:08 +0000
committerMike Bayer <mike_mp@zzzcomputing.com>2006-03-07 03:14:08 +0000
commit55ad2dc22a715bf38e070a5566e57e6228eaa0d7 (patch)
treee0e4465add1b82a35f82f02ecf735bf88fdc447d /examples/backref/backref_tree.py
parent2d596b61c06aca5409b4c219e267f152dc9e3293 (diff)
downloadsqlalchemy-55ad2dc22a715bf38e070a5566e57e6228eaa0d7.tar.gz
added backref() function, allows the creation of a backref where you also send keyword arguments that will be placed on the relation
Diffstat (limited to 'examples/backref/backref_tree.py')
-rw-r--r--examples/backref/backref_tree.py41
1 files changed, 41 insertions, 0 deletions
diff --git a/examples/backref/backref_tree.py b/examples/backref/backref_tree.py
new file mode 100644
index 000000000..09598069d
--- /dev/null
+++ b/examples/backref/backref_tree.py
@@ -0,0 +1,41 @@
+from sqlalchemy import *
+import sqlalchemy.attributes as attributes
+
+engine = create_engine('sqlite://', echo=True)
+
+class Tree(object):
+ def __init__(self, name='', father=None):
+ self.name = name
+ self.father = father
+ def __str__(self):
+ return '<TreeNode: %s>' % self.name
+ def __repr__(self):
+ return self.__str__()
+
+table = Table('tree', engine,
+ Column('id', Integer, primary_key=True),
+ Column('name', String(64), nullable=False),
+ Column('father_id', Integer, ForeignKey('tree.id'), nullable=True),)
+
+assign_mapper(Tree, table,
+ properties={
+ # set up a backref using a string
+ #'father':relation(Tree, foreignkey=table.c.id,primaryjoin=table.c.father_id==table.c.id, backref='childs')},
+
+ # or set up using the backref() function, which allows arguments to be passed
+ 'childs':relation(Tree, foreignkey=table.c.father_id, primaryjoin=table.c.father_id==table.c.id, backref=backref('father', uselist=False, foreignkey=table.c.id))},
+ )
+
+table.create()
+root = Tree('root')
+child1 = Tree('child1', root)
+child2 = Tree('child2', root)
+child3 = Tree('child3', child1)
+
+objectstore.commit()
+
+print root.childs
+print child1.childs
+print child2.childs
+print child2.father
+print child3.father