2

I'm using flask with py2neo for my Rest service , I have a user node with the label "User".

how to autoincrement id for the "User" label , in neo4j using py2neo?

1 Answers1

2

You don't, and you probably shouldn't. Neo4j already provides an internal id field that is an auto-incrementing integer. It isn't a property of the node, but is accessible via the id() function, like this:

MATCH (n:Person)
RETURN id(n);

So whenever you create any node, this already happens automatically for free by neo4j, and isn't done by py2neo.

If you need a different type of identifier for your code, I'd recommend something that's plausibly globally unique, like a UUID which is very easy to do in python, rather than an auto-incrementing integer.

The trouble with auto-incrementing numbers as IDs is that since they have a pattern to them (auto-incrementing) people come to rely on the value of the identifier, or come to rely on expectations of how the ID will be assigned. This is almost always a bad idea in databases. The sole purpose of the identifier is to be unique from everything else. It doesn't mean anything, and in some cases isn't even guaranteed not to change. Avoid embedding any reliance on any particular value or assignment scheme into your code.

That's why I like UUIDs, is because their assignment scheme is essentially arbitrary, and they clearly don't mean anything -- so they don't tempt designers to do anything clever with them. :)

FrobberOfBits
  • 17,634
  • 4
  • 52
  • 86
  • Can I get some clarification: I've read elsewhere online that we shouldn't be relying on Neo4j's internal ID it keeps for nodes and should instead use our own id, but then you mention here that we shouldn't be using our own id. Thanks! – Lamoni Apr 30 '15 at 15:28
  • @Lamoni this answer doesn't say you shouldn't use your own ID. Using your own ID is a good idea. Embedding meaning in that ID is a bad idea. (Hence UUIDs, which have no meaning, all they are is unique) – FrobberOfBits Apr 30 '15 at 17:16
  • ahh thank you for the clarification, my bad on misunderstanding. Thanks! – Lamoni May 01 '15 at 10:44