On 8/3/07, King Simon-NFHD78 <[EMAIL PROTECTED]> wrote:
> I think of adjacency lists and nested sets as more about hierarchies of
> a single type of object (imagine trying to represent a family tree with
> a row for each Person). I don't really think they're relevant in this
> case.

If I understand correctly the OP has a need to store a set of
hierarchical "Nodes". Only that some node types (video, image) can't
have children while others can (group, media_list).

Alexandre, why do you find that the nested set model suits your needs
better? The nested set model is particularly suited to the following
type of queries:
1. find all children of a particular node, without needing to know
their generation
2. test if node A is a descendant of node B

These are queries that are expensive with adjacency lists but
inexpensive with nested sets. Nested sets however may require you to
update a lot of nodes if your tree changes. Traditionally you would do
that with triggers or stored procedures - but those don't play well
with ORMs which assumes that an update of a row won't invalidate
cached instances of other rows.

Do you have specific needs for those kind of queries? I don't know
your application but I'd probably go with adjacency lists and a common
"Node" superclass that provides access to child and parent nodes.
Derive other classes from that and use joined table polymorphism as
Simon suggested.

There is another way to store hierarchical data than adjacency lists
and nested sets though. If you have an invariant id on your objects
(numerical or textual) you can store an objects "path", governed by

object.path = object.parent.path + delimiter + object.id

This might not be efficient for deeply nested trees maybe, but it can
help in answering the types of queries mentioned above efficiently
without requiring you to touch multiple rows upon inserting or
deleting. Also, it would be easy to implement in SA and you could have
all sorts of convenience methods on a Node class, like
get_direct_children
get_all_descendants
is_parent
is_child
is_descendant

Arnar

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