Neo4j.rb:在将一个节点替换为另一个节点之前,先转移所有关系 [英] Neo4j.rb : transfer all relationships before replacing a node by another
问题描述
在删除第一个节点之前,我试图将所有传入和传出关系(包括它们的属性)从一个节点转移到另一个节点.他们都有相同的标签.
I'm trying to transfert all ingoing and outgoing relationships (including their properties) from a node to another, before deleting the first one. they both have the same label.
讨论从这里开始: Neo4j Cypher:在替换节点之前转移所有关系另一个
node_query = Neo4j::Session.query.match(old_node: {uuid: node1.uuid}).match(new_node: {uuid: node2.uuid})
types = node_query.match('node-[rel]-()').pluck('DISTINCT type(rel)')
types.each do |type|
node_query.match('old_node-[rel]->(other)').with(:old_node, :rel, :other).create("new_node-[new_rel]->other").set('new_rel = rel').exec
node_query.match('old_node<-[rel]-(other)').with(:old_node, :rel, :other).create("new_node<-[new_rel]-other").set('new_rel = rel').exec
end
当我尝试实现该错误时
new_rel not defined (line 1, column 160)
"MATCH
(old_node {uuid: "YYYY"}),
(new_node {uuid: "XXXX"}),
oldnode-[rel]->(other)
WITH old_node, rel, other SET new_rel = rel
CREATE new_node-[new_rel]->other" ^
我尝试了另一种仅适用于Neo4j :: ActiveRel rels的方法,因为其他方法没有to_node属性.看来它还是不会复制关系属性:
I tried another way which works only for the Neo4j::ActiveRel rels, beacause for the others there's no to_node property. It seems that it doesn't copy the relationship properties anyway :
relations = old_node.rels(dir: :outgoing)
relations.each do |rel|
if defined? rel.to_node
new_node.create_rel(rel.type, rel.to_node, rel.props)
end
end
relations = self.rels(dir: :incoming)
relations.each do |rel|
if defined? rel.from_node
rel.from_node.create_rel(rel.type, new_node, rel.props)
end
end
推荐答案
嗯,现在我看到了错误,我想我知道原始答案有什么问题.我也会看看您的其他解决方案,但是这就是我将如何解决原始问题的方法(通过添加一个中断使SET
出现在CREATE
之后):
Ah, now that I see the error I think I know what was wrong with the original answer. I'll look at your other solution too, but here is how I would fix the original (by adding a break to make the SET
come after the CREATE
):
# Assuming node already loaded
node_query = Neo4j::Session.query.match(node: {neo_id: node.neo_id}, new_node: {neo_id: new_node.neo_id})
types = node_query.match('node-[rel]-()').pluck('DISTINCT type(rel)')
types.each do |type|
node_query.match('node-[rel:#{type}]->(other)').
where('other <> new_node').
with(:node, :new_node, :rel, :other).
create("new_node-[new_rel:#{type}]->other").
break.set('new_rel = rel').exec
node_query.match('node<-[rel:#{type}]-(other)').
where('other <> new_node').
with(:node, :new_node, :rel, :other).
create("new_node<-[new_rel:#{type}]-other").
break.set('new_rel = rel').exec
end
这篇关于Neo4j.rb:在将一个节点替换为另一个节点之前,先转移所有关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!