Search code examples
djangoneo4jneomodel

Neomodel relationships


I am new to Neo4J and graph databases in general and so I have some questions about structuring relationships. I am using Neomodel for this project.

I have two separate data model files (Note that I am using nodes.py instead of models.py as I experiment with Neo4J vs. Postgres)

accounts > nodes.py

class User(StructuredNode):
    firstName = StringProperty()
    lastName = StringProperty()
    email = StringProperty()
    active = BooleanProperty()
    campaigns = RelationshipTo('campaigns.nodes.Campaign', 'OWNS')

campaigns > nodes.py

class Campaign(StructuredNode):
    name = StringProperty()
    campaignId = IntegerProperty()
    active = BooleanProperty()
    user = RelationshipFrom('accounts.nodes.User', 'OWNS')

I am not sure that I have configured the relationships according to best practice (I am aware that the current code is redundant.) Is it necessary to have RelationshipTo and RelationshipFrom properties on both node classes? Or is RelationshipTo from the owner node (User) to the owned node (Campaign) sufficient?

Or should there be a separate RelationshipTo property from Campaign to User:

 user = RelationshipTo('accounts.nodes.User', 'OWNED_BY')

I'm not sure if this is even a significant issue, but any guidance or tips would be greatly appreciated!


Solution

  • You need a definition on each side only if you want to access it from both sides. I.e if you only want to be able to call campaign.user.single() from your campaign objects but don't care about accessing a users campaigns then you dont need a definition on a your User class.

    Generally I find it best to define a relationship on both sides as its easier for someone new to your schema to understand its structure. Both definitions should be of the same type, so OWNS_BY would return a different set of relationships. The naming of these types is totally up to you however uppercase seems to be the convention,

    Hope this helps, feel free to message me on github if you have any further questions,

    Cheers,

    Rob