mogui / pyorient

Orientdb driver for python that uses the binary protocol.
Apache License 2.0
167 stars 127 forks source link

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

Open cegprakash opened 6 years ago

cegprakash commented 6 years ago

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 commented 6 years ago

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

tommykennedy commented 6 years ago

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

cegprakash commented 6 years ago

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 commented 6 years ago

Thanks!

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

cegprakash commented 6 years ago

What is the error that you get?

On Tue, Aug 14, 2018, 2:49 PM Tom Kennedy notifications@github.com 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 .

tommykennedy commented 6 years ago

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 commented 5 years ago

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 commented 4 years ago

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