【问题标题】:Can you use MERGE with ON CREATE SET on a property with an existence constraint?您可以在具有存在约束的属性上使用 MERGE 和 ON CREATE SET 吗?
【发布时间】:2016-07-29 17:33:51
【问题描述】:

由于只有neo4j的企业版支持存在约束,我自己无法测试。

例如,假设我对关系属性 :Likes(createDate) 有一个存在约束,通常用于一个 :Person 喜欢另一个人的地方。

显然,由于存在约束,这样的创建调用会失败:

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 时还是在 ON CREATE 之后检查存在约束。看起来这将是一个相当标准的用例,我想知道企业版是否允许这样做没有问题。

【问题讨论】:

    标签: neo4j constraints cypher


    【解决方案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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-04-17
      • 1970-01-01
      • 2019-10-30
      • 2021-05-18
      • 1970-01-01
      • 2023-03-06
      • 1970-01-01
      相关资源
      最近更新 更多