由于只有neo4j的企业版支持存在限制,我不能自己测试。
作为一个例子,让我们说我对关系属性有一个存在约束,:喜欢(createDate),通常用于一个人:人喜欢另一个人。
显然,由于存在约束,这样的创建调用将失败:
MATCH (a:Person{id:1}), (b:Person{id:2})
CREATE (a)-[:Likes]->(b)
我必须创建与createDate属性的关系以符合约束。
但如果这种关系已经存在怎么办?我想做这样的事情:
MATCH (a:Person{id:1}), (b:Person{id:2})
MERGE (a)-[v:Likes]->(b)
ON CREATE SET v.createDate = timestamp()
我担心这不会被允许,但我不知道在MERGE时或者在创建之后是否检查了存在约束。这似乎是一个相当标准的用例,我想知道企业版是否允许这样做没有问题。
答案 0 :(得分:1)
是的,Neo4j在调用ON CREATE
后检查节点的状态并且它已完成:
neo4j-sh (?)$ CREATE CONSTRAINT ON ()-[r:Likes]-() ASSERT exists(r.createDate);
+-------------------+
| No data returned. |
+-------------------+
Property existence constraints added: 1
212 ms
neo4j-sh (?)$ CREATE (a:Person {uid: 1}), (b:Person {uid: 2});
+-------------------+
| No data returned. |
+-------------------+
Nodes created: 2
Properties set: 2
Labels added: 2
63 ms
neo4j-sh (?)$ MATCH (a:Person {uid: 1}), (b:Person {uid: 2}) MERGE (a)-[r:Likes]->(b);
+-------------------+
| No data returned. |
+-------------------+
Relationships created: 1
41 ms
ConstraintViolationException: Relationship 1398105 with type "Likes" must have the property "createDate" due to a constraint
neo4j-sh (?)$ MATCH (a:Person {uid: 1}), (b:Person {uid: 2}) MERGE (a)-[r:Likes]->(b) ON CREATE SET r.createDate = timestamp();
+-------------------+
| No data returned. |
+-------------------+
Relationships created: 1
Properties set: 1
43 ms
我正在使用Neo4j企业版3.0.3。