Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto increment property in Neo4j

As far as I understand it the IDs given by Neo4j (ID(node)) are unstable and behave somewhat like row numbers in SQL. Since IDs are mostly used for relations in SQL and these are easily modeled in Neo4j, there doesn't seem to be much use for IDs, but then how do you solve retrieval of specific nodes? Having a REST API which is supposed to have unique routes for each node (e.g. /api/concept/23) seems like a pretty standard case for web applications. But despite it being so fundamental, the only viable way I found were either via

  • language specific frameworks
  • as an unconnected node which maintains the increments:
// get unique id
MERGE (id:UniqueId{name:'Person'})
ON CREATE SET id.count = 1
ON MATCH SET id.count = id.count + 1
WITH id.count AS uid
// create Person node
CREATE (p:Person{id:uid,firstName:'Gabriel',lastName:'Smith'})
RETURN p AS person

Source: http://www.neo4j.org/graphgist?8012859

Is there really not a simpler way and if not, is there a particular reason for it? Is my approach an anti-pattern in the context of Neo4j?

like image 346
Gregor Weber Avatar asked Apr 03 '15 14:04

Gregor Weber


People also ask

What is id property in Neo4j?

Neo4j - ID Property. In Neo4j, "Id" is a default internal property for both Nodes and Relationships. That means, when we create a new Node or Relationship, Neo4j Database Server will assign a number for internal usage. It is incremented automatically.

How does the autoincrement property work?

If you add an auto-increment field to an existing table, the field automatically generates consecutive values and inserts them into the table. If you enable the AutoIncrement property for a field that already contains data, there must be no zero values in the field. The AutoIncrement property is designed to always assign numbers automatically.

How does Neo4j assign a number for internal usage?

That means, when we create a new Node or Relationship, Neo4j Database Server will assign a number for internal usage. It is incremented automatically. We will go through an example to understand this concept

What is the difference between PK and ID In Neo4j?

The ID that Neo4J assigns each object different than the PK assigned to a row in a database. the location in the Node index is reflected in the ID that Neo4J assigns. In a Relational Database the value is simply a unique and indexed key that you can look up against.


2 Answers

Neo4j internal ids are a bit more stable than sql row id's as they will never change during a transaction for e.g.

And indeed exposing them for external usage is not recommended. I know there are some intentions at Neo internals to implement such a feature.

Basically people tend to use two solutions for this :

  1. Using a UUID generator at the application level like for PHP : https://packagist.org/packages/rhumsaa/uuid and add a label/uuid unique constraint on all nodes.

  2. Using a very handful Neo4j plugin like https://github.com/graphaware/neo4j-uuid that will add uuid properties on the fly, so it remove you the burden to handle it at the application level and it is easier to manage the persistence state of your node objects

like image 182
Christophe Willemsen Avatar answered Sep 16 '22 14:09

Christophe Willemsen


I agree with Pavel Niedoba.

I came up with this without and UniqueID Node:

MATCH (a:Person)
WITH a ORDER BY a.id DESC LIMIT 1
CREATE (n:Person {id: a.id+1})
RETURN n

It requires a first Node with an id field though.

like image 44
mobin Avatar answered Sep 16 '22 14:09

mobin