简体   繁体   English

py2neo(Neo4j)自动增加属性?

[英]Auto increment property with py2neo (Neo4j)?

I'm using flask with py2neo for my Rest service , I have a user node with the label "User". 我将py2neo的烧瓶用于我的Rest服务,我有一个带有“用户”标签的用户节点。

how to autoincrement id for the "User" label , in neo4j using py2neo? 如何使用py2neo在neo4j中自动增加“用户”标签的ID?

You don't, and you probably shouldn't. 您不应该,也可能不应该。 Neo4j already provides an internal id field that is an auto-incrementing integer. Neo4j已经提供了一个内部id字段,该字段是一个自动递增的整数。 It isn't a property of the node, but is accessible via the id() function, like this: 它不是节点的属性,但是可以通过id()函数进行访问,如下所示:

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. 因此,无论何时创建任何节点,neo4j都会自动免费进行,而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. 如果您的代码需要其他类型的标识符,则建议您使用在全局上似乎唯一的标识符,例如在python中很容易实现的UUID ,而不是自动递增的整数。

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. 将数字自动递增为ID的麻烦在于,由于它们具有一种模式(自动递增),因此人们开始依赖标识符的值,或者开始依赖对ID分配方式的期望。 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. 这就是为什么我喜欢UUID的原因,因为它们的分配方案本质上是任意的,而且它们显然没有任何意义-因此,它们不会诱使设计师对其进行任何巧妙的处理。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM