のぶLab.

流しのソフトウェアエンジニアの雑記帳. Android, Scala, Clojure, Ruby on Railsなど

Neo4jで一意なpropertyを持つNodeを作る

アプリケーション内でuserのmailアドレスなど、あるLabelを持つnodeのpropertyに一意制約をつけるにはConstraintsを使う

http://neo4j.com/docs/stable/query-constraints.html

Cypherはこのような感じ

CREATE CONSTRAINT ON (user:User) ASSERT user.email IS UNIQUE

以下のような同じemail propertyを持つuserを複数回登録するとエラーになります

CREATE (user:User {name: "Taro", email: "test@example.com"})
CREATE (user:User {name: "Taro", email: "test@example.com"})

# => Node 3 already exists with label User and property "email"=[test@example.com]

また、この方法は'User' labelの付いているNodeのみ制限がかかるようになる。