这是错误 引起原因: org.neo4j.driver.exceptions.ClientException:等效约束已存在, 'Constraint( id=4, name='constraint_b7299877', type='NODE PROPERTY EXISTENCE', schema=(:profile {userName}) ) '.
public static void InitProfileDb() {
String queryStr;
try (Session session = ProfileMicroserviceApplication.driver.session()) {
try (Transaction trans = session.beginTransaction()) {
queryStr = "CREATE CONSTRAINT FOR (nProfile:profile) REQUIRE nProfile.userName IS NOT NULL";
trans.run(queryStr);
queryStr = "CREATE CONSTRAINT FOR (nProfile:profile) REQUIRE nProfile.password IS NOT NULL";
trans.run(queryStr);
// queryStr = "CREATE CONSTRAINT FOR (nProfile:profile) REQUIRE nProfile.userName IS UNIQUE";
// trans.run(queryStr);
trans.commit();
}
session.close();
}
}
异常意味着它所说的:约束已经存在,在同一标签/属性组合上不能有多个同类约束。
您可能需要考虑在约束创建查询中使用
IF NOT EXISTS
标志,这将允许它们成为无操作,并避免异常(如果约束已经存在)。
为了便于回顾,这里是我们的约束文档,其中包括该标志的用法:
https://neo4j.com/docs/cypher-manual/5/constraints/syntax/#constraints-syntax-create