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

has_many/has_one association does not preserve schema integrity #1548

Closed klobuczek closed 5 years ago

klobuczek commented 5 years ago
class Person
  include Neo4j::ActiveNode

  has_one :out, :parent, type: :parent, class: 'Person'
  has_many :in, :children, origin: :parent, class: 'Person'
end

root = Person.create!(children: [p1=Person.create!(children: [p2=Person.create!])])
root.update(children: [p1,p2])

Does not enforce the has_one :parent schema defintion.

Screen Shot 2019-05-07 at 10 21 49

@cheerfulstoic Do you recall if this has been missed or intentionally not implemented due to e.g. complexity etc.

Additional information which could be helpful if relevant to your issue:

Code example (inline, gist, or repo)

Runtime information:

Neo4j database version: neo4j gem version: neo4j-core gem version:

cheerfulstoic commented 5 years ago

Huh, that's an interesting one. I think that basically we didn't think about it.

One question that I had initially is: does this just happen in self-referencing associations. But I can imagine a situation where it could happen with two models referring to each other in this way.

My first instinct when thinking of it as just one model was to say that you should only ever update the has_one because I was thinking of ancestries in SQL where you have a parent_id and that's the one authoritative place that exists. But I think the same could be said of has_one/has_many associations on a pair of different models. So overall only ever updating the has_one is not reasonable.

Normally the origin key doesn't do much aside from save you a bit of time and be a bit more declarative. But I could imagine that if you set up a pair of associations via origin that the gem should probably take care of updating has_one associations correctly to respect the has_one