duanlongling5308 2015-08-11 16:35 采纳率: 0%
浏览 545
已采纳

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

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

graph

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")
  • 写回答

2条回答 默认 最新

  • dongyou5271 2015-08-12 15:24
    关注

    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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型