neo4jrb / activegraph

An active model wrapper for the Neo4j Graph Database for Ruby.
http://neo4jrb.io
MIT License
1.4k stars 276 forks source link

Duplicate relationship between nodes while creating them #1574

Open mrhardikjoshi opened 4 years ago

mrhardikjoshi commented 4 years ago

When we initialize two nodes with mentioning relationship on both side, It creates two identical relationship between these two nodes. Should it only create one relationship instead?

Code example (inline, gist, or repo)

require 'neo4j'
require 'neo4j/core/cypher_session/adaptors/http'

puts "neo4j-core version: #{Neo4j::Core::VERSION}"
puts "neo4j version: #{Neo4j::VERSION}"
puts "Ruby version: #{RUBY_VERSION}"

neo4j_adaptor = Neo4j::Core::CypherSession::Adaptors::HTTP.new('http://neo4j:mypass@localhost:7474')
Neo4j::ActiveBase.on_establish_session { Neo4j::Core::CypherSession.new(neo4j_adaptor) }
Neo4j::ActiveBase.current_session = Neo4j::Core::CypherSession.new(neo4j_adaptor)

class Teacher
  include Neo4j::ActiveNode

  property :name, type: String
  has_many :in, :students, origin: :teacher
end

class Student
  include Neo4j::ActiveNode

  property :name, type: String
  has_one :out, :teacher, type: :teacher
end

teacher = Teacher.new(name: 'Dronacharya')
student = Student.new(name: 'Arjun', teacher: teacher)
teacher.students = [student]
teacher.save
teacher_reloaded_obj = Teacher.find teacher.id
puts "stundends associated with teacher: #{teacher_reloaded_obj.students.pluck(:uuid)}"

Runtime information

Output:

neo4j-core version: 9.0.0 neo4j version: 9.6.0 Ruby version: 2.5.3 stundends associated with teacher: ["7aee154e-39ec-461c-93ee-96d7bc25f0fb", "7aee154e-39ec-461c-93ee-96d7bc25f0fb"]

Screenshot 2019-10-13 at 10 18 34 AM
OpenCoderX commented 4 years ago

Check out the docs on creating unique associations: https://neo4jrb.readthedocs.io/en/stable/ActiveNode.html#creating-unique-relationships