首页 > 解决方案 > 无法创建约束

问题描述

我的数据看起来像:

    user_id                             from_page_id        to_page_id          event_type  timestamp
0   USER355A155307089387404434774906    pages/mine/mine     pages/mine/mine     pv          1598936406406
1   USER355A155307089387404434774906    pages/mine/mine     /pages/score/score  pv          1598936408261
2   USER355A155307089387404434774906    pages/mine/mine     pages/mine/mine     click       1598936408311
3   USER355A155307089387404434774906    pages/mine/mine     /pages/score/score  click       1598936410824
4   USER355A155307089387404434774906    pages/score/score   /pages/scoreDetai   pv          1598936410878
...

我正在尝试创建一个图表以将用户与其访问的页面路径联系起来:

LOAD CSV WITH HEADERS FROM "file:///data.csv" AS line
CREATE (u:User), (p1:Page {from: line.from_page_id}), (p2:Page {to: line.to_page_id}),
(u)-[:VIEWS]->(p1), (u)-[:VIEWS]->(p2), (p1)-[:NEXT]->(p2);
CREATE CONSTRAINT ON (u:User) ASSERT u.id IS UNIQUE;

它创建图表,但它给了我错误:

Neo.DatabaseError.Schema.ConstraintCreationFailed: Unable to create Constraint( name='constraint_47e7f15d', type='UNIQUENESS', schema=(:User {id}) ):
Both Node(324) and Node(327) have the label `User` and property `id` = 'USER0138119929164105321108358370'

我在这里做错了什么?

标签: neo4j

解决方案


这有什么帮助:

在单独的过程中创建约束.. 它不必是您的密码的一部分。请注意,您只能在没有重复项的情况下创建 CONSTRAINT。

一旦 CONSTRAINT 存在,Neo4j 将检查并防止插入任何重复项。

当您拥有数据时,您应该使用

MERGE (u:User {user_id:line.user_id}) 

更多信息:https ://neo4j.com/docs/cypher-manual/current/clauses/merge/


推荐阅读