Sagar
Sagar

Reputation: 11

How do you add edges to existing vertices in Gremlin-python?

What is the standard way to add edges between two already existing vertices in gremlin-python? I've tried numerous iterations including the two code versions below, but unfortunately they all lead to errors.

Code snippet 1:

   v1= g.V().has("Customers", "OrderedId",1).next()
   v2 = g.V().has("Hotels", "Id", 1).next()
   e_id = v1.addE('HasVisited').to(v2).toList()

Error 1:

AttributeError: 'Vertex' object has no attribute 'addE'

Code Snippet 2

v1= g.V().has("Customers", "OrderedId",1).next()
v2 = g.V().has("Hotels", "Id", 1).next()
g.V(v2).as_('t').V(v1).addE("HasVisited").to("t").toList()

Error 2

KeyError: None

Upvotes: 1

Views: 591

Answers (1)

stephen mallette
stephen mallette

Reputation: 46206

I would probably just do:

g.V().has("Customers", "OrderedId",1).as_('a').
  V().has("Hotels", "Id", 1).as_('b').
  addE("HasVisited").from_('a').to("b").iterate()

The first snippet you presented is simply an invalid API. You can't operate on a Vertex object directly and you should think of it as read-only. The second snippet, I'm less sure about. If g.V(v2) and g.V(v1) both independently return values, I would have expected that snippet to have created an edge.

Upvotes: 2

Related Questions