Hi! I've posted this question over on Stack Overflow -- thought
perhaps the mailing list might be a better resource for help.  (If
you're a Stack Overflow user and want the points, here's the link:
http://stackoverflow.com/questions/1791713/creating-container-relationship-in-declarative-sqlalchemy
)

My Python / SQLAlchemy application manages a set of nodes, all derived
from a base class Node. I'm using SQLAlchemy's polymorphism features
to manage the nodes in a SQLite3 table. Here's the definition of the
base Node class:

class Node(db.Base):
    __tablename__ = 'nodes'
    id = Column(Integer, primary_key=True)
    node_type = Column(String(40))
    title = Column(UnicodeText)
    __mapper_args__ = {'polymorphic_on': node_type}
and, as an example, one of the derived classes, NoteNode:

class NoteNode(Node):
    __mapper_args__ = {'polymorphic_identity': 'note'}
    __tablename__ = 'nodes_note'
    id = Column(None,ForeignKey('nodes.id'),primary_key=True)
    content_type = Column(String)
    content = Column(UnicodeText)

Now I need a new kind of node, ListNode, that is an ordered container
of zero or more Nodes. When I load a ListNode, I want it to have its
ID and title (from the base Node class) along with a collection of its
contained (child) nodes. A Node may appear in more than one ListNode,
so it's not a proper hierarchy. I would create them along these lines:

note1 = NoteNode(title=u"Note 1", content_type="text/text",
content=u"I am note #1")
session.add(note1)

note2 = NoteNode(title=u"Note 2", content_type="text/text",
content=u"I am note #2")
session.add(note2)

list1 = ListNode(title=u"My List")
list1.items = [note1,note2]
session.add(list1)

The list of children should only consist of Node objects -- that is,
all I need is their base class stuff. They shouldn't be fully realized
into the specialized classes (so I don't get the whole graph at once,
among other reasons).

I started along the following lines, cobbling together bits and pieces
I found in various places without a complete understanding of what was
going on, so this may not make much sense:

class ListNode(Node):
    __mapper_args__ = {'polymorphic_identity': 'list',
'inherit_condition':id==Node.id}
    __tablename__ = 'nodes_list_contents'
    id = Column(None, ForeignKey('nodes.id'), primary_key=True)
    item_id = Column(None, ForeignKey('nodes.id'), primary_key=True)
    items = relation(Node, primaryjoin="Node.id==ListNode.item_id")

This approach fails in several ways: it doesn't appear to allow an
empty ListNode, and setting the items attribute to a list results in
SQLAlchemy complaining that 'list' object has no attribute
'_sa_instance_state'. Not surprisingly, hours of random mutations on
this theme haven't given any good results,

I have limited experience in SQLAlchemy but really want to get this
working soon. I'd very much appreciate any advice or direction you can
offer. Thanks in advance!

--

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


Reply via email to