Bill
Bill

Reputation: 25555

Neo4j: Proper way to model relationship between contacts and users

This is my scenario (using a completely fictitious application):

At this point, I want to connect up this new user to the existing contact that Joe created (based on email address) and allow the user to update the contact information if needed. An extra wrinkle is that this user may have signed up for contests with different companies and so there may be N contact objects that I now want to coalesce into a single contact object owned by the user.

Here is the model that I've described in Cypher (2 contacts created by different people, _3 and _6, and then user is created with matching email, _1):

CREATE (_1:User { email:"[email protected]", name:"Jack" }),
       (_2:Phone { no:"555-5555" }),
       (_3:Contact { name:"John", email:"[email protected]" }),
       (_4:User { email:"[email protected]",name:"David" }),
       (_5:Phone { no:"555-5555" }),
       (_6:Contact { name:"John", email:"[email protected]" }),
       (_7:User { email:"[email protected]",name:"Fred" }),
        _3-[:primary_phone]->_2,
        _6-[:primary_phone]->_5,
        _4-[:created]->_3,
        _7-[:created]->_6

Any thoughts on the best way to handle this situation? I'm trying to figure out how David and Fred to know that the contact they created as John actually wants to be called Jack.

Should I remove the contact nodes and move all the relationships to the newly created user node? Should I just add a relationship from the user node to all of the existing contact nodes?

Once a user is created, all future relationships will be directly to the user. I just need to support the scenario where everything still needs to work even if the specified user hasn't signed up yet (or never signs up).

Thanks!

Upvotes: 0

Views: 272

Answers (2)

eighteyes
eighteyes

Reputation: 1314

It seems like Phone nodes are needlessly complicating things when it could just be a User and a Contact nodes with phone information stored as a property. That way you don't have to worry about updating multiple records for each contact.

Your John > Jack scenario should be handled as a note property on the Contact node.

Upvotes: 0

p3rnilla
p3rnilla

Reputation: 351

I would go for a new relationship SAME_AS for now: http://console.neo4j.org/r/iangl3

Should I remove the contact nodes and move all the relationships to the newly created user node? Should I just add a relationship from the user node to all of the existing contact nodes?

I would go for the last one - You should add a relationship from the user node that all of the existing contact nodes are. This so you don't lose the value of the alias who are in users contact lists, then you let your users have the choice to name their own contacts to what ever they prefer.

Upvotes: 1

Related Questions