0

I am trying to dynamically create the name of a field in a CSV that I am parsing using FOREACH.

I am trying this:

// From Load CSV 
WITH row,
    ['NAME-A', 'NAME-B'] AS olink_panels
FOREACH (panel in olink_panels |
    MERGE (p:Plate      {plate_id:  row["Prefix $panel-Suffix"], 
                        name:       panel})
)

Neo4j is parsing the code but not creating any new nodes. I suspect it is not evaluating the $panel variable.

Rajendra Kadam
  • 4,004
  • 1
  • 10
  • 24
  • Could the nodes already exist? If this executed successfully once then it's likely it's just matching to existing nodes rather than finding none and creating them instead. – InverseFalcon Jun 10 '19 at 17:17

1 Answers1

0

If you are trying to use panel to generate a header name, this should work:

FOREACH (panel in ['NAME-A', 'NAME-B'] |
    MERGE (p:Plate     {plate_id:  row["Prefix " + panel + "-Suffix"], 
                        name:      panel})
)

The $panel syntax (with the starting dollar sign) should only be used if panel was passed to the query as a parameter. And Cypher would not do parameter substitution within string literals anyway.

cybersam
  • 63,203
  • 6
  • 53
  • 76
  • That makes sense about the `$panel` syntax. I tried your suggestion @cybersam and it parses, but does not create the nodes. There are no :Plate nodes in my graph before or after running the code. I put the array outside of the FOREACH block and aliased it it with any success. It is like the alias for `row` and `panel` are not being evaluated or are out of scope somehow. – David Hughes Jun 10 '19 at 20:57
  • Can you add to your question what the first 3 or so lines in your CSV file look like? – cybersam Jun 10 '19 at 21:17
  • In trying to understand why the `panel` variable is not being evaluated, tried the following: ``` // Sample -> Batch WITH row MATCH (s:Sample {barcode: row.Patient_Group}) MATCH (b:Batch {name: row.Batch}) MERGE (s)-[:HAS_BATCH]->(b) WITH ['A', 'B'] as tests, row FOREACH (test in tests | MERGE (t:TEST {name: test}) )``` the FOREACH block is run after the last MERGE. The FOREACH block in is the same location in my code, at the end. It does not eval `test`. If I run FOREACH block by itself it does eval `test`. – David Hughes Jun 10 '19 at 22:38
  • Sorry about the code formatting...I'll try to fix. Unfortunately I can not share any records from the file. I am successfully building nodes and relationships from rows in the file. Just getting hung up on the FOREACH loop. I also tried UNWIND. – David Hughes Jun 10 '19 at 22:44
  • If a `MATCH` clause fails to match, then the entire query is aborted. That is apparently what is causing your issue. Your question should have provided your entire query. – cybersam Jun 11 '19 at 00:26
  • I should have posted the query, but I think the issue is more to do with the FOREACH suboutine. The reason I suspect this is when I run this code all properties are created except `val`. – David Hughes Jun 11 '19 at 13:16
  • `FOREACH (panel in panels | //MERGE (p:Plate {plate_id: row["Olink " + panel + "-Plate ID"]}) //ON CREATE SET p.name = panel MERGE (t:DEBUG) SET t.sex = row.SEX SET t.panel = panel SET t.val = row["Olink " + panel + "-Plate ID"] )` – David Hughes Jun 11 '19 at 13:16
  • Comments in Stackoverflow omit all line breaks, so I do not know where your Cypher comment lines end and therefore do not know what your actual queries look like. For example, in your comment from 20 hours ago, I do not know if the `MATCH` clauses are commented out. Can you amend your Question? – cybersam Jun 11 '19 at 19:37