Search code examples
sql-serverneo4jrelational-databasedata-modelinggraph-databases

Converting a sample relational data model to graph data model


I have designed a relational data model and it's graph data model. I want to know whether I had done it in a correct way and whether my graph data model is correct or not. If there is anything wrong or ambiguous with my model please leave a comment.

As you can see in the graph data model there are 4 labels:

  1. Company
  2. User
  3. Skill
  4. Project

And you can see that each node with it's label has it's properties and the join tables are transformed into relationships between the nodes. I want to know what should I do with the "Primary keys" like userID or SkillID.

Relational data model:

enter image description here

Graph Data model:

enter image description here


Solution

  • About the primary keys question:

    In fact Neo4j nodes and relationships have an internal unique ID that can be accessed using the id() function:

    match (d)-[r]-()
    return id(d) as nodeId, id(r) as relId
    

    However your application should not rely on these ids because they are reused by Neo4j when a node or relationships is deleted. The Neo4j documentation says:

    Neo4j reuses its internal ids when nodes and relationships are deleted. This means that applications using, and relying on internal Neo4j ids, are brittle or at risk of making mistakes. It is therefore recommended to rather use application-generated ids.

    More here.

    So, if you really want a primary key, I think you have two main options:

    1. Manage the primary keys at application level. That is: create and assign unique ids in the application code that access your Neo4j database.

    2. Use GraphAware UUID plugin. GraphAware UUID is a simple library that transparently assigns a UUID to newly created nodes and relationships in the graph and makes sure nobody can (accidentally or intentionally) change or delete them.