espeed / bulbs

A Python persistence framework for graph databases like Neo4j, OrientDB and Titan.
http://bulbflow.org
Other
623 stars 82 forks source link

Proposal for modelling relationships with Nodes #73

Open barbogast opened 11 years ago

barbogast commented 11 years ago

I'm developing an application with specific types of Nodes. I'm able to model the nodes using the Node class, but I cannot model which node type may have a relationship with which node type.

I was thinking of defining the relationships along with the properties:

class Category(Node):
    element_type = 'category'
    label = String(nullable=False)

class Tag(Node):
    element_type = 'tag'
    label = String(nullable=False)

class Issue(Node):
    element_type = 'issue'
    text = String(nullable=False)
    category = Relationship('has_category', Category, multi=False, nullable=False)
    tags = Relationship('has_tag', Tag, multi=True)

>>> category1 = Category(label='Proposal')
>>> tag1 = Tag(label='Proposal')
>>> tag2 = Tag(label='Question')

>>> issue1 = Issue(text='Modelling relationships would be nice', 
...                    category=category1,
...                    tags=[tag1, tag2])

>>> # it could also be possible to append multi-relationships afterwards
>>> tag3 = Tag(label='xxx')
>>> issue1.tags.append(tag3)

>>> # an Exception would be raised if a relationship is missing which is not nullable
>>> issue2 = Issue(text='Category is missing here')
ValueError('This Node must have a least one relation to <Category>')

I need to control the relationships between the nodes so I need to implement something like this. If you like the idea I could try to get it into bulbs, so you could merge it. If you dont think that it fits into bulbs, I would write it on top of bulbs I dont have to maintain a fork of bulbs.

Details:

espeed commented 11 years ago

I'm on vacation, but go for it if you want. My other thought is to put the node type constraints on the relationship model. On Jan 1, 2013 8:41 AM, "Koblaid" notifications@github.com wrote:

I'm developing an application with specific types of Nodes. I'm able to model the nodes using the Node class, but I cannot model which node type may have a relationship with which node type.

I was thinking of defining the relationships along with the properties:

class Category(Node): element_type = 'category' label = String(nullable=False) class Tag(Node): element_type = 'tag' label = String(nullable=False) class Issue(Node): element_type = 'issue' text = String(nullable=False) category = Relationship('has_category', Category, multi=False, nullable=False) tags = Relationship('has_tag', Tag, multi=True)

category1 = Category(label='Proposal')>>> tag1 = Tag(label='Proposal')>>> tag2 = Tag(label='Question') issue1 = Issue(text='Modelling relationships would be nice', ... category=category1,... tags=[tag1, tag2])

it could also be possible to append multi-relationships afterwards>>> tag3 = Tag(label='xxx')>>> issue1.tags.append(tag3)

an Exception would be raised if a relationship is missing which is not nullable>>> issue2 = Issue(text='Category is missing here')ValueError('This Node must have a least one relation to ')

I need to control the relationships between the nodes so I need to implement something like this. If you like the idea I could try to get it into bulbs, so you could merge it. If you dont think that it fits into bulbs, I would write it on top of bulbs I dont have to maintain a fork of bulbs.

Details:

  • It should be possible to have to different relationship types between the same node types.
  • How can 2 Nodes have relationships to each other? The class of the second node wont be available when the first one ist created. One solution could be to add the relationship of the first node after the second node is created.

    — Reply to this email directly or view it on GitHubhttps://github.com/espeed/bulbs/issues/73.

espeed commented 11 years ago

see https://github.com/espeed/bulbs/issues/74#issuecomment-18270205