Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to set vertex or edge ID #98

Open
mbbfyb opened this issue Feb 17, 2018 · 5 comments
Open

Ability to set vertex or edge ID #98

mbbfyb opened this issue Feb 17, 2018 · 5 comments

Comments

@mbbfyb
Copy link

mbbfyb commented Feb 17, 2018

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
Copy link
Author

mbbfyb commented Feb 17, 2018

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
Copy link
Owner

davebshow commented Feb 17, 2018

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
Copy link
Author

mbbfyb commented Feb 17, 2018

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
Copy link
Owner

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
Copy link
Author

mbbfyb commented Feb 18, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants