2016-05-30 5 views
0

2つのノードUserとPostがあるとしましょう。関係、upvote、として存在することができると言う(:ユーザー) - [UPVOTED] - >(:ポスト)py2neoを使って特定の関係を削除する

非空のリストで、次のクエリの結果は、その後、私は関係を削除する場合:

 rel_list = list(graph.match(start_node=user, end_node=post, rel_type="UPVOTED") 
     if len(rel_list) > 0: 
      # delete the relationship from the graph 
     else: 
      # create the relationship 
      rel = Relationship(user, "UPVOTED", post) 
      graph.create_unique(rel) 

答えて

0

リレーションシップを削除するには、separateメソッドを使用する必要があります。http://py2neo.org/v3/database.html#py2neo.database.Transaction.separate。あなたはその上でいくつかの議論のためのGitHubでこの問題に見てみたいことがあり

https://github.com/nigelsmall/py2neo/issues/508

よろしく、自分の質問に答える

+0

AttributeError: 'Graph'オブジェクトに属性 'separate'がありません – karzler007

+0

これは、 'separate'が' Graph'ではなく 'Transaction'オブジェクトのメソッドであるためです。 'tx = graph.begin()'でトランザクションを開き、 'tx.separate(your_relationship)'を実行し、最後に 'tx.commit() 'でコミットする必要があります。 – sancho

+0

しかし、 'オブジェクトには属性' begin 'があ​​りません。それは少し奇妙です。 – karzler007

0

が、これは同様に他の人に役立ちます願っています。

rel_list = list(graph.match(start_node=user, end_node=post, rel_type="UPVOTED") 
    if len(rel_list) > 0: 
     # delete the relationship from the graph 
     # fetch the ID of the relationship -> rel_id 
     query = ''' 
     MATCH (a:NeoZotr)-[r:LIKED]->(b:NeoZot) 
     WHERE ID(r) = {x} 
     DELETE r 
     ''' 
     graph.cypher.execute(query,x=rel_id) 
    else: 
     # create the relationship 
     rel = Relationship(user, "UPVOTED", post) 
     graph.create_unique(rel) 

あなたは例外TypeError(私の場合にあったように)で終わる場合、あなたがフェッチrel_idは、Unicodeのスタイルにあった可能性があるかもしれません。その場合はint(rel_id)を渡します。

関連する問題