唯一性约束不会阻止在Neo4j上添加相同的数据。

huangapple go评论71阅读模式
英文:

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上添加相同的数据。

编辑:

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
  • email
  • 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

唯一性约束不会阻止在Neo4j上添加相同的数据。

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)

huangapple
  • 本文由 发表于 2015年8月12日 00:35:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/31947316.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定