I am trying to add a property for a specific vertex type. Assuming I have person
and car
vertex types in my graph schema, how can I add the name
and birthday
properties only to the person
vertex?
Example of creating firstName
property:
graph.openManagement().makePropertyKey('firstName')
.dataType(String.class).cardinality(Cardinality.SINGLE).make();
Here I am creating new property of vertex but How can I restrict it to a specific type of vertex?
Thanks in advance.
With the new JanusGraph 0.3.0 release, it is now possible to create schema constraints which do exactly what you want. Since those constraints are disabled by default, they first need to be enabled by setting schema.constraints
to true
. Now you can create a constraint like this:
mgmt = graph.openManagement()
person = mgmt.makeVertexLabel('person').make()
name = mgmt.makePropertyKey('firstName').dataType(String.class).
cardinality(Cardinality.SINGLE).make()
mgmt.addProperties(person, name)
mgmt.commit()
which means that the firstName
property key can only be used on a vertex with the label person
.
When you now try to add this property to a vertex with a different label, then it will throw an exception:
gremlin> g.addV('car').property('firstName','test')
Property Key constraint does not exist for given Vertex Label [car] and property key [firstName].
See the official JanusGraph documentation on schema constraints for more information.
I updated my answer as it stated before that schema constraints would not yet be possible which they now are.