davebshow / goblin

A Python 3.5 rewrite of the TinkerPop 3 OGM Goblin
Other
93 stars 21 forks source link

Ability to set vertex or edge ID #98

Open mbbfyb opened 6 years ago

mbbfyb commented 6 years ago

In looking through the code for goblin and aiogremlin I can't see to figure out how to set the vertex or edge ID to a user defined value.

I would presume it would be something as simple as

class Person(goblin.Vertex):
    id = goblin.Property(goblin.String)
    # or
    _id = goblin.Property(goblin.String)

But in looking at this file there seems to be a lot of special handling for the id values. I found this which lead me down the path of something similar to

class Person(goblin.Vertex):
    id = goblin.Property(properties.IdProperty(data_type=goblin.String), default='abc123')

but the id doesnt seem to stick when I set the vertex and then check the id value.

Can you point me in the right direction?

mbbfyb commented 6 years ago

Given that that last bit of code doesn't actually work, I tried something similar to:

class CustomId(properties.IdProperty, abc.DataType):
    def validate(self, val):
        if val is not None:
            try:
                return str(val)
            except ValueError as e:
                raise exception.ValidationError(
                    'Not a valid string: {}'.format(val)) from e
    def to_db(self, val=None):
        return super(CustomId, self).to_db(val=val)
    def to_ogm(self, val):
        return super(CustomId, self).to_ogm(val)

class Person(goblin.Vertex):
    id = goblin.Property(CustomId(data_type=goblin.String), default='abc123')

but I am really just grasping at straws here.

davebshow commented 6 years ago

Well, as far as the goblin.Element interface is concerned, all instantiated Goblin elements already have an id property:

In [1]: from goblin import Vertex

In [2]: class Person(Vertex): pass

In [3]: p = Person()

In [4]: p.id = 1

In [5]: p.id
Out[5]: 1

Goblin currently doesn't support custom id data types. It possibly could in the future. May I ask why you need this functionality? What provider are you working with? Typically they will handle element ids...

mbbfyb commented 6 years ago

That makes sense, thanks for the response.

My use case is that I have objects with a uid that I was hoping to use as the authoritative vertex/edge identifier to keep things consistent across the platform. The same uid in my graph db corresponds to other things in different data stores (like Mysql, AWS S3 key, etc), and I was just trying to keep things the same.

As an easy solution, I can create my own custom property uid and map it to that, but it just wont match my other datastores in terms of the authoritative identifier of the vertex or edge.

I am working with Amazon Neptune which returns a random uuid when you create a vertex or edge, so I was trying to control that. Running a simple create vertex function sets the id of the object to something similar to

>>> leif.id
'acb0d299-68c3-2687-5cf8-3bc9edf99280'

I am not running the latest goblin due to Neptune's limitations if that makes a difference.

Here are my relevant pip packages:

aiogremlin==3.2.4
goblin==2.0.0
gremlinpython==3.3.1
davebshow commented 6 years ago

What if you simply set the id with your own uuid and then save per usual?

leif.id = 'myobjectsuuid'
session.add(leif)
await session.flush()

I ask because I truly don't know...

mbbfyb commented 6 years ago

I thought it would be that easy originally, but it does not seem to be the case.

>>> import goblin
>>> import asyncio
>>> from goblin import Goblin
>>>
>>> class Person(goblin.Vertex):
...     car = goblin.VertexProperty(goblin.String)
...
>>> async def store_element(app, vertex1):
...     session = await app.session()
...     session.add(vertex1)
...     await session.flush()
...
>>> loop = asyncio.get_event_loop()
>>> app = loop.run_until_complete(Goblin.open(loop, hosts=["neptune_host"]))
>>> app.register(Person)
>>>
>>> p1 = Person()
>>> p1.id = "abc123"
>>> p1.id
'abc123'
>>>
>>> loop.run_until_complete(store_element(app, p1))
>>> print(p1.id)
0eb0d4ec-f29c-0fc8-d282-f69ad3575497

EDIT: The behavior is the same with or without setting the id to a goblin.String property, like shown in the original comment on this ticket.