[英]Neo4J COALESCE in relationship
I am trying to run a relationship creation but for some reason I cant use coalesce inside of my relationship query.我正在尝试创建关系,但由于某种原因,我无法在我的关系查询中使用合并。 Im completely stuck and some of this may be malformed but im trying to fix it one step at a time.我完全卡住了,其中一些可能格式不正确,但我试图一步一步地修复它。 here is my code这是我的代码
/* Get the kill assassin and student by relation UUID*/
'MATCH (a:Student)-[r:TARGET]->(t:Student) WHERE r.uuid = $uuid ' +
/* Lets Kill the student and set confirmed to true */
'SET r.confirmed = true, t.IsDead = true WITH ' +
/* Delete any teachers that may have the target of the student*/
'MATCH (t)<-[tr:TARGET]-(:Teacher) DELETE r ' +
/* Get the assassinated persons target. Set them as X. */
'MATCH (t)-[ar:TARGET]->(x:Student) WHERE ar.confirmed = false AND x.IsDead = false ' +
/* Lets deal with anyone who inherited our target if we are dead :(*/
'OPTIONAL MATCH (t)<-[sr:TARGET]-(ss:Student) WHERE sr.confirmed = false AND ss.IsDead = false ' +
/* Lets steal their kill node and set them as our new target unless of course someone decided to kill us and nab them */
'CREATE (COALESCE(t,a))-[nr:TARGET {killed:false,confirmed:false}]->(t) '
EDIT: I cleaned up my code A LOT.编辑:我清理了我的代码很多。 Just for the record coalesce cant be used in a relationship.只是为了记录 coalesce 不能在关系中使用。 Here is the cleaned up code for those wondering.这是那些想知道的人的清理代码。
/**
* a: The student who made the kill
* v: The victim of the kill
* r: the relationship between a and t
* x: the victims target
* tr (OPTIONAL) : the teacher who we hired to kill our target
* ss: The person who will be inheriting t(victim)'s target
*/
/* Get the Killer, set them as A AND set the Victim as T */
'MATCH (a:Student)-[r:TARGET {uuid:$uuid,confirmed:false}]->(v:Student),' +
/* Fetch the Victims Target and set them as x */
'(v)-[:TARGET {confirmed:false}]->(x:Student {IsDead:false}) ' +
/* Check if we hired a teacher to assassinate our target. Set the relationship to TR (teacher relation)*/
'OPTIONAL MATCH (v)<-[tr:TARGET]-(:Teacher) ' +
/* Fetch the alive person who has the target (in-case we died and then it went into review) */
'MATCH (v)<-[sr:TARGET {confirmed:false}]-(ss:Student {IsDead:false}) ' +
/* Create a relationship between SS and t */
'CREATE (ss)-[:TARGET {killed:false,confirmed:false}]->(x) ' +
/* Set the Kill Relationship to complete and kill the victim */
'SET r.confirmed = true, v.IsDead = true,a.Balance = a.Balance + 3 ' +
/* Delete the teacher relationship */
'DELETE tr ', {uuid:uuid}
You can't use an expression inside that CREATE
- but you can move that expression into a WITH
statement to assign it to a variable, and then use that variable in your create.您不能在CREATE
使用表达式 - 但您可以将该表达式移动到WITH
语句中以将其分配给一个变量,然后在您的创建中使用该变量。
The last line could be replaced by:最后一行可以替换为:
/* Lets steal their kill node and set them as our new target unless of course someone decided to kill us and nab them */
WITH COALESCE(t, a) as n, t
CREATE (n)-[nr:TARGET {killed:false,confirmed:false}]->(t)
Which compiles fine, but there are some other problems in the query I had to fix up first:哪个编译得很好,但是我必须先修复查询中的其他一些问题:
SET r.comfirmed...
ends in a WITH
but there's no variables after it第 2 行, SET r.comfirmed...
以WITH
结尾,但后面没有变量MATCH (t)<-...
ends in a DELETE
but the next line starts with a MATCH
- you need a WITH
in between the two第 3 行, MATCH (t)<-...
以DELETE
结尾,但下一行以MATCH
开头 - 您需要在两者之间WITH
So the query I end up with is:所以我最终得到的查询是:
MATCH (a:Student)-[r:TARGET]->(t:Student) WHERE r.uuid = $uuid
/* Lets Kill the student and set confirmed to true */
SET r.confirmed = true, t.IsDead = true
/* Delete any teachers that may have the target of the student*/
WITH a, t, r
MATCH (t)<-[tr:TARGET]-(:Teacher) DELETE r
/* Get the assassinated persons target. Set them as X. */
WITH a, t
MATCH (t)-[ar:TARGET]->(x:Student) WHERE ar.confirmed = false AND x.IsDead = false
/* Lets deal with anyone who inherited our target if we are dead :(*/
OPTIONAL MATCH (t)<-[sr:TARGET]-(ss:Student) WHERE sr.confirmed = false AND ss.IsDead = false
/* Lets steal their kill node and set them as our new target unless of course someone decided to kill us and nab them */
WITH COALESCE(t, a) as n, t
CREATE (n)-[nr:TARGET {killed:false,confirmed:false}]->(t)
But even then it looks wrong - the COALESCE
will always resolve to t
because to have gotten that far in the query t
must have a value, so the last CREATE
will just be creating a relationship between t
and itself.但即便如此,它看起来也是错误的COALESCE
总是会解析为t
因为在查询中达到那么远的t
必须有一个值,所以最后的CREATE
只会在t
和它自身之间创建一个关系。 Also: the OPTIONAL MATCH
introduces the new variable ss
but then the variable isn't used anywhere else (making the optional match somewhat redundant).另外: OPTIONAL MATCH
引入了新变量ss
但该变量没有在其他任何地方使用(使可选匹配有些多余)。
I'm guessing the COALESCE
should be between ss
and a
, rather than t
and a
but it's hard to tell.我猜COALESCE
应该在ss
和a
之间,而不是t
和a
但很难说。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.