On Apr 22, 2008, at 8:57 PM, David Gardner wrote:

>
> Thanks for your response, I ended up deciding that it was OK if leaf
> nodes show up as regular nodes. So I dropped my qry_node query, and  
> kept
> the qry_leaf query, and it is working now.
>
> I should have mentioned as a side note, that my SA code needs to play
> nice with a closed source package, so I am not able to make schema  
> level
> changes.
>


just FTR, a working version of the "two selects" mapping:


from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.orm.collections import attribute_mapped_collection

m = MetaData(create_engine('sqlite://', echo=True))

node_table = Table("n", m,
     Column('id', Integer, primary_key=True),
     Column('name', String(50)),
     Column('type', String(50)),
     Column('parent_id', Integer, ForeignKey('n.id'))
)
m.create_all()

qry_node = select ([node_table], node_table.c.type! 
='file').alias('node_query')
qry_leaf = select ([node_table],  
node_table.c.type=='file').alias('leaf_query')

class Node(object):
     def __init__(self, name):
         self.type='node'
         self.name = name

class Leaf(object):
     def __init__(self, name):
         self.type='file'
         self.name = name

mapper(Leaf, qry_leaf)

mapper(Node, qry_node, properties = {
   'Children' : relation(Node,  
cascade='all',collection_class=attribute_mapped_collection('name'),
     backref=backref('Parent', remote_side=[qry_node.c.id])
   ),
   'Files' :  
relation(Leaf,primaryjoin=(qry_node.c.id==qry_leaf.c.parent_id),
             order_by=qry_leaf.c.name, cascade='all,delete-orphan')
})

n1 = Node('n1')
n1.Parent = Node('n1parent')
n1.Files.append(Leaf('file1'))
n1.Files.append(Leaf('file2'))

sess = create_session()
sess.save(n1)
sess.flush()

sess.clear()

files =  
sess.query(Node).filter_by(name='n1parent').one().Children['n1'].Files
assert [f.name for f in files] == ['file1', 'file2']

--~--~---------~--~----~------------~-------~--~----~
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