pyorient icon indicating copy to clipboard operation
pyorient copied to clipboard

graph.create_edge object has no attribute '_id'

Open cegprakash opened this issue 6 years ago • 8 comments

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?

cegprakash avatar Aug 02 '18 13:08 cegprakash

https://stackoverflow.com/questions/51703088/create-an-edge-in-orientdb-without-using-raw-query

cegprakash avatar Aug 06 '18 08:08 cegprakash

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

tommykennedy avatar Aug 13 '18 10:08 tommykennedy

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)

cegprakash avatar Aug 13 '18 11:08 cegprakash

Thanks!

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

tommykennedy avatar Aug 14 '18 09:08 tommykennedy

What is the error that you get?

On Tue, Aug 14, 2018, 2:49 PM Tom Kennedy [email protected] wrote:

Thanks!

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

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/mogui/pyorient/issues/274#issuecomment-412809264, or mute the thread https://github.com/notifications/unsubscribe-auth/ABOgiBpsh9svx8ZYmoYTlqnnzwtc1vK5ks5uQpYpgaJpZM4VsS_A .

cegprakash avatar Aug 14 '18 09:08 cegprakash

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?

tommykennedy avatar Aug 14 '18 21:08 tommykennedy

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

nikulukani avatar Apr 08 '19 22:04 nikulukani

Hey,

I just signed the petition "Nitin Gadkari: Ban the honking in India unless absolutely Necessary !!" and wanted to see if you could help by adding your name.

Our goal is to reach 100 signatures and we need more support. You can read more and sign the petition here:

http://chng.it/xxdV9V9CRR

Thanks! Prakash

cegprakash avatar Jan 27 '20 13:01 cegprakash