Skip to content

Returning null on other relationships only when MERGE relationship inside UNWIND #2550

@venkat125

Description

@venkat125

I'm using @Query on Repository for updating motherTongue and known languages similar model in IT + motherTongue in model with added toDelete transient field on Knows model class

Trying to update both relationships in single query as below:

@Query("MATCH (f:Person {id: $from})\n" +
		"OPTIONAL MATCH (mt:Language {name: $mot})\n" +
		"OPTIONAL MATCH (f)-[fmtl:MOTHER_TONGUE_IS]->(mt)\n" +
		"UNWIND $relations As obj WITH obj, f, mt, fmtl\n" +
			"CALL apoc.do.when(obj.__properties__.toDelete,\n" +
				"'OPTIONAL MATCH (f)-[fkl:KNOWS]->(l:Language {name: o.__properties__.__target__.__id__}) DELETE fkl RETURN f',\n" +
				"'OPTIONAL MATCH (l:Language {name: o.__properties__.__target__.__id__}) \n" +
					"MERGE (f)-[fklCr:KNOWS]->(l) RETURN f,fklCr,l',\n" +
			"{f:f, o:obj}) YIELD value\n" +
		"WITH f, mt, fmtl, collect(value.fklCr) AS fklCr, collect(value.l) AS l\n" +
			"CALL apoc.do.when(fmtl IS NOT NULL,\n" +
				"'RETURN f, fmtl, mt',\n" +
				"'OPTIONAL MATCH (f)-[fmtlTd:MOTHER_TONGUE_IS]->(:Language) DELETE fmtlTd MERGE (f)-[fmtlCr:MOTHER_TONGUE_IS]->(mt) RETURN f,fmtlCr,mt',\n" +
			"{f:f, fmtl:fmtl, mt:mt}) YIELD value\n" +
	   "RETURN f, fklCr, l, mt, fmtl, collect(value.fmtlCr)")
Person updateRel(@Param("from") String from, @Param("relations") List<Knows> relations, @Param("mot") String motherTongue);

when toDelete: true the result is

{"id":"xyxy","name":"Helge","knowsMt":{"id":14,"language":{"name":"English"}},"knownLanguages":[]}

whereas, when toDelete: false, it does creates know language as "German" but the other relationships are null. In this case: knowMt (Mother Tongue) is null (not retrieved from DB)

{"id":"xyxy","name":"Helge","knowsMt":null,"knownLanguages":[{"id":17,"description":null,"toDelete":null,"language":{"name":"German"}}]}

But could see mother tongue ('xyxy')-[:MOTHER_TONGUE_IS]->('English') relationship created in DB.
Note:

  1. This is not only for the same TargetNode even when there are any other relationship to different Labelled Target, those are returning null only.
  2. Its working fine with multiple relationships with different targets, when used without UNWIND in query. Issue is faced only when mixing UNWIND for iterate the list along with other relationship updates.

Just tagging meistermeier as you would be more familiar.
Please suggest me if the above query is having any defect. Thanks Team

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions