ruby-on-railsneo4jassociationsneo4j.rb

Duplicate edges with same properties neo4j


I have two nodes between which, same edge with same property is being created over and over again. How can I avoid this? If the edges have different properties, its ok and it needs to be kept but if the properties are same, there should be one edge only.

EDIT: I'm using rails and I want to do this through application and not Cypher query.

EDIT: Sharing some code for relevance:

dis = Disease.where(disease: params[:disease]).first
fac = Factor.where(factor: params[:factor])
dis.factors.create(fac, prop: "p1")

So, what I want is if I input same disease and factor, it not duplicate the edge (which it is currently doing) as property being set is also same. However, if in future, this p1 changes to p2, then the edge should be added.

Refer post Neo4j inconsistent behaviour of model classes for model classes (Disease and factor).


Solution

  • You have two options. You could use the unique option on your association(s):

    http://neo4jrb.readthedocs.io/en/8.1.x/ActiveNode.html#creating-unique-relationships

    This allows you to specify anything from there being only one of that relationship type between two nodes (regardless of properties), to only creating unique nodes if all properties are exactly the same. If you create an ActiveRel model, you can also do the same thing with the creates_unique declaration:

    http://neo4jrb.readthedocs.io/en/8.1.x/ActiveRel.html#creating-unique-relationships