Skip to content

Instantly share code, notes, and snippets.

@dstagner
Created January 27, 2014 15:54
Show Gist options
  • Save dstagner/8651121 to your computer and use it in GitHub Desktop.
Save dstagner/8651121 to your computer and use it in GitHub Desktop.
Multi-key constraints in Neo4J
Problem: In a data structure, sometimes we want constraints on more than one key. But right now, the Neo4J 2.0 uniqueness constraint really only supports uniqueness on one key.
Example: Parents (the meatspace kind) can have several children. None of their children should have the same name. However, their children may have the same name as children of other parents. So you don't want uniqueness on names for all children, but you want uniqueness on names for children of specific parents. This is two-key uniqueness.
Victor has three children - Mary, Alice, and Quasimodo. And Marissa has three children - Xena, Bob, and Alice. Victor's Alice and Marissa's Alice are two different children, but have the same name.
Solution: Create a unique key on parents (like a uuid, since parents shouldn't be unique on name). Then create a unique key on child that's a combination of parent uuid and child name. Kind of a hack, but it should work.
More later when I actually write an implementation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment