Py2neo - update an existing node with new uniqueness-limited properties (merge_one)

I tried to figure out a way to adapt the merge_one functionality from py2neo v2 to v3. I read to the Google group : "The merge_one method no longer exists in v3, since you should use merging in all cases." but I can't figure out how easy it is to use regular merge in v3.

I am trying to recreate the Nicole White neo4j twitter project with a slight modification. She used merge_one. There are uniqueness restrictions (u: User) - u.username and (t: Tweet) - t.id. Her script always has the same properties for the Tweet and User nodes, but I make the case when sometimes I want to return and add additional properties to an existing merged node. However, I get an error

py2neo.database.status.ConstraintError: Node 178 already exists with label Tweet and property "id"=[***]

I understand this because when I have, for example, a tweet that already exists only with id, and then I try to do

        tw_dict = {'id'=t['id'], 'text':t['text'], 'created':t['created_at'],
               'rts':t['retweet_count'],
               'favs':t['favorite_count'], 'lang':t['lang']}
        tweet = Node("Tweet", **tw_dict)
        graph.create(tweet)

merge does not find the same tweeter with all these properties, and when he tries to create it, it works with a unique constraint in the Tweet id. It seems like the merge_one function would solve this, but it is not available in v3. So instead, I did the following:

    exists = graph.find_one("Tweet", "id", t['id'])
    if exists:
        exists['text'] = t['text']
        exists['created'] = t['created_at']
        exists['rts'] = t['retweet_count']
        exists['favs'] = t['favorite_count']
        exists['lang'] = t['lang']
    else:
        tw_dict = {'text':t['text'], 'created':t['created_at'],
               'rts':t['retweet_count'],
               'favs':t['favorite_count'], 'lang':t['lang']}
        tweet = Node("Tweet", **tw_dict)
        graph.create(tweet)

. py2neo - node - ( id)? , Cypher , , , py2neo. -, node, .

+4
2

py2neo v3 graph.merge(), .

node graph.merge(), , node.push(). graph.merge_one(), .

from py2neo import Graph, Node
graph = Graph()

tweet = Node('Tweet', id=123)
graph.merge(tweet)
tweet['text'] = 'Hello World'
tweet.push()

script, py2neo v3; .

+7

;

1). tweet.push() . graph.push().

2). , :

transaction = graph.begin()
transaction.merge(tweet)
transaction.graph.push(tweet)
transaction.commit()

graph.merge transaction.merge?

+4

All Articles