Darren Ford
Darren Ford

Reputation: 251

How do I assign an id to CosmosDB graph edge when I created it?

I see in the docs that it mentions UserSuppliedIds is supported for edges but any edge I create ignores my .property assignment for id and assigns a guid. I don't want to add duplicate edges between two vertices so I was going to assign my own id to it( and then I can quickly and efficiently for query it using regular sql syntax too). How can I use my own 'id' for an edge?

Upvotes: 3

Views: 1145

Answers (3)

Oliver Towers
Oliver Towers

Reputation: 445

I have tested the following gremlin expression which adds an edge and assigns a custom id.

g.V('1231234').addE('postedTo').property('id', '1231234:post_4').from(g.V('post_4'))

This worked using the latest and previous versions of Microsoft.Azure.Graph nuget package (0.2.4-preview and 0.2.2-preview):

Note: Edge or vertex id property can only be assigned when creating the element via addV or addE operations. After the element is written, the id property is read-only.

I did not get time to test this on a graph server instance, however version 0.2.2-preview of the package should be comparable to what is deployed so I'm expecting the same results.

Upvotes: 1

Jesse Carter
Jesse Carter

Reputation: 21147

Since it doesn't appear to be supported today through the Gremlin APIs I'd suggest you take a look at using the Document APIs for CRUD operations against your graph elements. This is the approach I've taken at work and we've had great success with it. Basically, if you insert a few vertices and edges through Gremlin and then inspect the resulting documents using SQL in the portal you'll be able to see the format that is expected in the underlying storage.

Building on that, we've designed some libraries that take the POCOs for our various Vertex and Edge types and translate them into the graph format expected in the backend by Cosmos. This will allow you to completely control the selection of Ids for your edges. A very important and common use case that you've pointed out which is also important to our system is the ability to prevent more than one edge for a particular vertex by restricting it's Id.

Upvotes: 0

Jayanta Mondal
Jayanta Mondal

Reputation: 456

Which docs are you referring to? Is it the gremlin docs?

Note that, we currently control the ids of the edges ourselves so that the edges could be collocated with the source vertices for query-efficiency reasons. And it's a bug that we don't throw an exception when an edge id is indeed provided.

We are changing this behavior and will allow users to specify id while creating an edge. I will check with the team, and get you an ETA for this.

Thanks again for reporting this. Please, let us know if we can help in any other way.

Jayanta

Upvotes: 1

Related Questions