Hi Emerson, Over the last couple of weeks, I have been working on an implementation of n-ary relationships on top of Neo4j. I also detailed how n-ary relationships could in principle be implemented in the database kernel (see: http://lists.neo4j.org/pipermail/user/2011-August/011191.html).
Right now I am working on traversals for n-ary relationships, in an attempt to remove the unnaturalness you describe. If we look at your example and using the nomenclature of Enhanced-API, you'd have an EdgeType "REFERS" with three ConnectorsTypes: "Referrer", "Referree", "Course", such that we can create the following Egde: REFERS Referrer --paul Referree -- john Course -- history A traversal takes as input a Vertex (strictly speaking a Traversal, of which a Vertex is a subclass), and takes two ConnectorTypes to traverse from a Vertex to an Edge to a Vertex. So if you want to know the Referrers for the course "history", the traversal would be defined like: //create a traversal description TraversalDescription descr = TraversalDescription.add(Course, Referrer) //traverse the graph based on the description starting from the vertex "history" descr.traverse(history) Ther traverse method returns a Iterable<Path>, which in this case contains only one Path. The path consists of two Connections, (history, Course) and (paul, Referrer). I hope this somehow answers your question. Niels > Date: Sun, 14 Aug 2011 18:57:22 +0200 > From: [email protected] > To: [email protected] > Subject: [Neo4j] n-ary relationships > > Hi, > > I started looking into Neo4j this morning, and played with some domain > models to see whether it really passes a "whiteboard friendliness" test. I'm > really after a persistence solution that makes it straightforward to persist > a domain model designed from a DDD perspective, and Neo4j is looking > promising so far. The one aspect that's not too clear is how to model n-ary > relationships, and I'm curious as to how those of you with experience with > Neo4j and graph databases would approach it. > > An edge connects two vertices, so in a graph database, a relationship > connects two nodes. But when modeling, there are frequently relationships > between multiple entities. For example, student John attends a History > course at university, and John was referred to the History course by Paul. > This relationship relates John, Paul, and the History course. > > There are a few ways I can think of to model this. > > 1) A node John has an ATTENDS relationship with node History, and the > relationship has a "referrer" property with Paul's ID. Simple, but keeping > IDs as properties seems like an anti-pattern. > 2) A node Referral has a CREATED_BY relationship with node Paul, > a FOR_COURSE relationship with node History, and a TO_STUDENT relationship > with node John. It's effectively a three-way join. > 3) Same as 2, but with an additional ATTENDS relationship between John and > History. This is particularly useful if a course attendant may attend a > course without being referred. > > This might not be the best example in the world, but it should drive my > point home: when relationships have a degree higher than 2, relationships > need to be modelled as vertices to overcome the binary nature of edges. Is > this expected behavior that's part and parcel of graph databases, or am I > approaching the modeling incorrectly somehow? > > My concern is that traversals may become unnatural when this happens. Say I > want to iterate over the attendants of a course, and show the name of who > referred them when I do so. Will I have the graph database equivalent of n+1 > selects because the data I want to extract (referrer name) is in a different > node (Paul) to my node of interest (John), instead of in the relationship to > it (attends)? > > Any tips and opinions would be appreciated. > > Cheers, > Emerson > _______________________________________________ > Neo4j mailing list > [email protected] > https://lists.neo4j.org/mailman/listinfo/user _______________________________________________ Neo4j mailing list [email protected] https://lists.neo4j.org/mailman/listinfo/user

