Search code examples
neo4jpy2neo

py2neo 2.x, "Local entity is not bound to a remote entity"


When updating Neo4j and py2neo to last versions (2.2.3 and 2.0.7 respectively), I'm facing some problems with some import scripts.

For instance here, just a bit of code.

graph = py2neo.Graph()
graph.bind("http://localhost:7474/db/data/")
batch = py2neo.batch.PushBatch(graph)

pp.pprint(batch)

relationshipmap={}

def create_go_term(line):
    if(line[6]=='1'):
        relationshipmap[line[0]]=line[1]
    goid = line[0]
    goacc = line[3]
    gotype = line[2]
    goname = line[1]

    term = py2neo.Node.cast( {
        "id": goid, "acc": goacc, "term_type": gotype, "name": goname
    })

    term.labels.add("GO_TERM")

    pp.pprint(term)

    term.push()
    #batch.append( term )

    return True


logging.info('creating terms')
reader = csv.reader(open(opts.termfile),delimiter="\t")
iter = 0
for row in reader:
    create_go_term(row)
    iter = iter + 1
    if ( iter > 5000 ):
        # batch.push()
        iter = 0

# batch.push()

When using batch or simply push without batch, I'm getting this error:

py2neo.error.BindError: Local entity is not bound to a remote entity

What am I doing wrong?

Thanks!


Solution

  • I think you first have to create the node before you can add the label and use push:

    term = py2neo.Node.cast( {
        "id": goid, "acc": goacc, "term_type": gotype, "name": goname
    })
    
    graph.create(term) # now the node should be bound to a remote entity
    
    term.labels.add("GO_TERM")
    
    term.push()
    

    Alternatively, you can create the node with a label:

    term = Node("GO_TERM", id=goid, acc=goacc, ...)
    graph.create(term)