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

graph.create_edge object has no attribute '_id' #274

Open
cegprakash opened this issue Aug 2, 2018 · 8 comments
Open

graph.create_edge object has no attribute '_id' #274

cegprakash opened this issue Aug 2, 2018 · 8 comments

Comments

@cegprakash
Copy link

I don't like raw queries. So I'm trying to create an edge using

graph.create_edge(Friend, orientRecord1, orientRecord2)

But I get AttributeError: 'OrientRecord' object has no attribute '_id'

because orientRecord does not have an _id in it. What am I doing wrong?

@tommykennedy
Copy link

Do you have any example project to see how you implemented the OGM with Django?

@cegprakash
Copy link
Author

cegprakash commented Aug 13, 2018

Here are my class structures

class Person(Node):
    id = String(unique = True)
    name = String()
    pass


class Friend(Relationship):
    pass

Here is a query that I use to fetch orientRecords and trying to create an edge

data = client.command("SELECT FROM Person WHERE name = 'Prakash'")
print(len(data))
if len(data) == 1:
    prakash = data[0]
    print(prakash.oRecordData['name'])

data = client.command("SELECT FROM Person WHERE name = 'Brian'")
print(len(data))
if len(data) == 1:
    brian = data[0]
    print(brian.oRecordData['name'])


#cmd = graph.create_edge_command(Friend, prakash, brian)

@tommykennedy
Copy link

Thanks!

Im struggling with the connection settings for orient. How did you set up your settings.py file?

@cegprakash
Copy link
Author

cegprakash commented Aug 14, 2018 via email

@tommykennedy
Copy link

tommykennedy commented Aug 14, 2018

I am not at the stage of getting an error. I am new to Django and I am struggling to set up the connections to the database. Is it possible to share a sample of how to configure this?

@nikulukani
Copy link
Contributor

The graph.create_edge command expects objects that are subclasses of vertex, and not raw OrientRecords.

For your code, one way to use create_edge would be along the following lines

graph.create_edge(Friend, graph.get_element(prakash._rid), graph.get_element(brian._rid))

However, I would suggest to modify your code so that you use the OGM exclusively and do no mix low-level queries with the OGM unless absolutely necessary.

from pyorient.ogm import Graph, Config
from pyorient.ogm.declarative import declarative_node, declarative_relationship
from pyorient.serializations import OrientSerialization

Node = declarative_node()
Relationship = declarative_relationship()

class Person(Node):
    element_type = 'Person'
    element_plural = 'Persons'
    id = String(unique = True)
    name = String()

class Friend(Relationship):
    label = 'Friend'

graph = Graph(Config.from_url('<db_url>','<uname>', '<pass>', initial_drop=False,
                                     serialization_type=OrientSerialization.Binary))

# If the classes already exists in your db schema, use include instead of create_all
# which will be a lot faster.
# Even if the classes do not exist, you only need to run create_all the first time after
# adding any additional classes above. Use graph.include once the classes are
# created in your db
graph.create_all(Node.registry)
graph.create_all(Relationship.registry)
# graph.include(Node.registry)
# graph.include(Relationship.registry)

prakash = graph.Persons.create(name='Prakash', id='1')
brian = graph.Persons.create(name='Brian', id='2')

prakash2 = graph.Persons.query(name='Prakash').first()
brian2 = graph.Persons.query(name='Brian').first()

graph.Friend.create(prakash, brian)
# graph.Friend.create(prakash2, brian2)  # Same as above

@cegprakash
Copy link
Author

cegprakash commented Jan 27, 2020 via email

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

3 participants