英文:
Uniqueness constraint does not prevent from adding same data on Neo4j
问题
我想创建一个带有用户的neo4j
数据库。我希望以下属性是唯一的:
- 用户名
- 电子邮件
- 令牌
我尝试过以下方法:
CREATE CONSTRAINT ON (user:User) ASSERT user.username IS UNIQUE
CREATE CONSTRAINT ON (user:User) ASSERT user.email IS UNIQUE
CREATE CONSTRAINT ON (user:User) ASSERT user.token IS UNIQUE
然而,这并不能阻止我创建具有重复电子邮件、用户名或令牌的新节点。图中显示了5个节点,所有节点的数据都相同,但只有第一个节点是用户。我不希望出现这种情况,我希望neo4j
返回一个错误。
这种情况是否可能?
谢谢
编辑:
Neo4j
版本:2.2.3
我使用Go
中的neoism
插入数据:
n, err := db.CreateNode(neoism.Props{"id": user.Id, "username" : user.Username,
"displayname" : user.Displayname,
"email" : user.Email, "token" : user.Token})
if err != nil {
return ERROR_NEO4J
}
n.AddLabel("User")
英文:
I want to create a neo4j
database with users. I want the following properties to be unique:
- username
- token
What I've tried:
CREATE CONSTRAINT ON (user:User) ASSERT user.username IS UNIQUE
CREATE CONSTRAINT ON (user:User) ASSERT user.email IS UNIQUE
CREATE CONSTRAINT ON (user:User) ASSERT user.token IS UNIQUE
However, this does not prevent me from creating new nodes with repeated email, username or token. The image shows 5 nodes, all with the same data but only the first one is a User. I don't want this, I want neo4j
to return an error.
Is it possible?
Thanks
EDIT:
Neo4j
version: 2.2.3
And I use neoism
for Go
to insert data:
n, err := db.CreateNode(neoism.Props{"id": user.Id, "username" : user.Username,
"displayname" : user.Displayname,
"email" : user.Email, "token" : user.Token})
if err != nil {
return ERROR_NEO4J
}
n.AddLabel("User")
答案1
得分: 1
一个唯一性约束与一个标签和属性对相关联。你所有的唯一性约束都涉及到User
标签,所以neo4j将只对User
节点强制执行唯一性。
如果你认为合适的话,你可以修改你的唯一性约束,使其涉及到其他标签(比如Base
),并将该标签分配给所有你的节点。neo4j允许一个节点具有多个标签,所以你仍然可以继续使用User
标签(但不能作为约束的一部分)。
英文:
A uniqueness constraint is associated with a label and property pair. All of your uniqueness constraints involve the User
label, so neo4j will just enforce uniqueness on User
nodes.
If you think it is appropriate, you can modify your uniqueness constraints so that they involve some other label (say, Base
), and assign that label to all your nodes. neo4j allows a node to have multiple labels, so you can keep using the User
label as well (but not as part of a constraint).
答案2
得分: 1
我最终通过使用neoism
的原始查询解决了这个问题。原始代码创建了一个没有标签的节点,稍后再添加标签。此时,约束条件不允许代码添加标签,但节点已经创建。
解决方案是运行一个查询,在创建节点的同时添加标签:
cq := neoism.CypherQuery {
Statement: `CREATE (n:User {id:{id}, username:{username},
displayname:{displayname}, email:{email},
token:{token}}) RETURN n`,
Parameters: neoism.Props {
"id": user.Id,
"username" : user.Username,
"displayname" : user.Displayname,
"email" : user.Email,
"token" : user.Token,
},
}
err := db.Cypher(&cq)
英文:
I finally solve it with a raw query with neoism
. The original code created a node with no label and it was added later. At this moment, the constraint didn't allow the code to add the label but the node was already created.
The solution is running a query that adds the label at the same time it creates the node:
cq := neoism.CypherQuery {
Statement: `CREATE (n:User {id:{id}, username:{username},
displayname:{displayname}, email:{email},
token:{token}}) RETURN n`,
Parameters: neoism.Props {
"id": user.Id,
"username" : user.Username,
"displayname" : user.Displayname,
"email" : user.Email,
"token" : user.Token,
},
}
err := db.Cypher(&cq)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论