You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The problem occurs when using the same relation class for both :in and :out relations.
I am building a tree structure using ActiveGraph::Nodes, and use ActiveGraph::Relationship for the relations between the nodes (to store some information in the relations).
class NodeWithRel
include ActiveGraph::Node
has_one :out, :parent, rel_class: :NodeRel
has_many :in, :subnodes, rel_class: :NodeRel
property :title
end
class NodeRel
include ActiveGraph::Relationship
from_class :NodeWithRel
to_class :NodeWithRel
end
When I add a subnode to an existing node, the existing node's parent relation is deleted when it is saved to the database:
I think the reason might be that when the relation is set up, ActiveGraph::Relationship first deletes any existing relations that are :has_one relation.
In active_graph/relationship/persistance.rb, line 55:
def delete_has_one_rel
to_node.delete_reverse_has_one_relationship(self, :in, from_node) if to_node.persisted?
from_node.delete_reverse_has_one_relationship(self, :out, to_node) if from_node.persisted?
end
In active_graph/node/has_n.rb, line 244:
def delete_reverse_has_one_relationship(relationship, direction, other_node)
rel = relationship_corresponding_rel(relationship, direction, other_node.class)
delete_has_one_rel!(rel.last) if rel && rel.last.type == :has_one
end
and line 254:
def relationship_corresponding_rel(relationship, direction, target_class)
self.class.associations.find do |_key, assoc|
assoc.relationship_class_name == relationship.class.name ||
(assoc.relationship_type == relationship.type.to_sym && assoc.target_class == target_class && assoc.direction == direction)
end
end
When using ActiveGraph::Relationship, #relationship_corresponding_rel doesn't consider the direction of the relation when selecting, so when there are two relations using the same relationship class (as in my tree example), both relationships are returned, and one of them (the last) is deleted by #delete_reverse_has_one_relationship.
Changing line 256 from: assoc.relationship_class_name == relationship.class.name ||
to: (assoc.relationship_class_name == relationship.class.name && assoc.direction == direction) ||
does fix the problem for me, but I am not sure if this change has any other side effects.
The problem occurs when using the same relation class for both :in and :out relations.
I am building a tree structure using ActiveGraph::Nodes, and use ActiveGraph::Relationship for the relations between the nodes (to store some information in the relations).
When I add a subnode to an existing node, the existing node's parent relation is deleted when it is saved to the database:
This doesn't happen if I use "ordinary" relations instead of ActiveGraph::Relationship:
I think the reason might be that when the relation is set up, ActiveGraph::Relationship first deletes any existing relations that are :has_one relation.
In active_graph/relationship/persistance.rb, line 55:
In active_graph/node/has_n.rb, line 244:
and line 254:
When using ActiveGraph::Relationship, #relationship_corresponding_rel doesn't consider the direction of the relation when selecting, so when there are two relations using the same relationship class (as in my tree example), both relationships are returned, and one of them (the last) is deleted by #delete_reverse_has_one_relationship.
Changing line 256 from:
assoc.relationship_class_name == relationship.class.name ||
to:
(assoc.relationship_class_name == relationship.class.name && assoc.direction == direction) ||
does fix the problem for me, but I am not sure if this change has any other side effects.
Runtime information:
Neo4j database version: 4.0.11
activegraph
gem version: 10.0.2neo4j-ruby-driver
gem version: 1.7.4The text was updated successfully, but these errors were encountered: