I am trying to model graph data structure where nodes can be connected
by different types of edges.
For example, we could have 2 nodes represnting persons: "Alice" and
"Bob" and we could have 3 nodes representing projects: "projectA",
"projectB", "projectC".
Alice is working on projectA and projectC.
Bob is working on projectB and projectC.
Alice is manager of projectA.
Bob is manager of projectB.

I would like to encode all this in 2 database tables:

Node table:
id      name
1       Alice
2       Bob
3       projectA
4       projectB
5       projectC

Edge table:
n1_id   n2_id   edge_type
1       3       working_on
1       5       working_on
2       4       working_on
2       5       working_on
1       3       manager_of
2       4       manager_of

(it would even by better to replace the edge_type by an edge_type_id
and move them to a separate table, but I would already be very happy
to get it to work like this)

and I would like to be able to access all this as a dictionary of
lists (or sets):
>>> alice = session.query(Node).filter_by(name="Alice").first()
>>> bob = session.query(Node).filter_by(name="Bob").first()

>>> alice.related['working_on']
[<Node: 'projectA'>, <Node: 'projectC'>, ]

>>> bob.related['working_on']
[<Node: 'projectB'>, <Node: 'projectC'>, ]

>>> alice.related['manager_of']
[<Node: 'projectA'>, ]

>>> bob.related['manager_of']
[<Node: 'projectB'>, ]

and adding stuff:
>>> alice.related['working_on'].append(Node("projectD"))
>>> alice.related['working_on']
[<Node: 'projectA'>, <Node: 'projectC'>, <Node: 'projectD'>, ]

I think this must be doable using the association_proxy in some way
(it's vaguely familiar to the Broker/Stock/Holding example in the
documentation, but now the extra value in the linking table (shares)
should be used as key in the dictionary) but I can't get it to work.
Could somebody give me an idea if this is at all possible and maybe a
push in the right direction?

Many thanks,

Jan.

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