scholrly / neo4django

Drop-in Neo4j/Django integration.
GNU General Public License v3.0
358 stars 83 forks source link

Relationships .remove not working because of LazyNode #199

Closed tonjo closed 11 years ago

tonjo commented 11 years ago

Relationship .remove works ONLY when related node is just created, not when recalling a previously defined one. (That's why unit test don't fail)

I consider it a crucial issue.

Inside the remove method, the problem is rels_by_node[obj.node] returns [] because when the node "obj" is recalled through get or get_or_create, obj.node is a neo4jrestclient.client.LazyNode, not a neo4jrestclient.client.Node. When it is created it's a Node instead.

Creating a node:

n=Person(name='nick') n.id 50 type(n) <class 'neo4jrestclient.client.Node'> n.friendTo.add(someone) n.save() Trying manually, rels_by_node[n] gives something: [<Neo4j Relationship: http://localhost:7474/db/data/relationship/89>]

But when I search for the very same Node:

b=Person.objects.get(id=50) type(b.node) <class 'neo4django.db.models.script_utils.LazyNode'> and rels_by_node[n.node] []

Printing out rels_by_node:

key: < Neo4j Node: http://localhost:7474/db/data/node/50 > value: [ < Neo4j Relationship: http://localhost:7474/db/data/relationship/89 > ] key: < Neo4j Node: http://localhost:7474/db/data/node/44 > value: [ < Neo4j Relationship: http://localhost:7474/db/data/relationship/79 > ] key: < Neo4j LazyNode: http://localhost:7474/db/data/node/50 > value: []

The FIRST row is the one giving the relationship, but is the last one found, because of LazyNode.

From row 730 in relationships.py: for obj in objs: candidate_rels = rels_by_node[obj.node] if hasattr(obj, 'node') else []

It will be empty if obj.node is LazyNode

            if candidate_rels:
            # always False, and then an exception raises when trying to remove
            # from self._added which is empty.

I wish I can help more, but I'm not sure what a LazyNode is ;) I don't know exactly where to touch.

mhluongo commented 11 years ago

Good catch. neo4django.db.models.script_utils.LazyNode is a subclass of neo4jrestclient's Node that only pulls data from its initial representation, instead of every time a property is read. I'll try to look into this soon.

pirhoo commented 11 years ago

Any news about this ?

Cheers

tonjo commented 11 years ago

Hi, I Just came back, I hope in a few days I will have some time. Thanks for the patience. Il giorno 01/ago/2013 18:31, "Pierre Romera" notifications@github.com ha scritto:

Any news about this ?

Cheers

— Reply to this email directly or view it on GitHubhttps://github.com/scholrly/neo4django/issues/199#issuecomment-21949427 .