activegraph icon indicating copy to clipboard operation
activegraph copied to clipboard

Wrong reuse of identical node aliases in generated cypher query.

Open klobuczek opened this issue 5 years ago • 1 comments

The following code

Role.all.branch { lookup.where(id: 1) }.branch { lookup.where(id: 2) }

generates the following cypher:

  MATCH (n:`Role`)
  MATCH (n)-[rel1:`lookup`]->(result_lookups:`Lookup`)
  WHERE (ID(result_lookups) = {ID_result_lookups})
  MATCH (n)-[rel3:`lookup`]->(result_lookups:`Lookup`)
  WHERE (ID(result_lookups) = {ID_result_lookups})
  RETURN n | {:ID_result_lookups=>2}

which is wrong in 2 accounts

  1. Reuse of the result_lookups for both match conditions.
  2. Reuse of the same parameter name ID_result_lookups

Please note that the relationships are aliased correctly with rel1 and rel3.

The correct cypher query should be:

  MATCH (n:`Role`)
  MATCH (n)-[rel1:`lookup`]->(result_lookups1:`Lookup`)
  WHERE (ID(result_lookups1) = {ID_result_lookups1})
  MATCH (n)-[rel3:`lookup`]->(result_lookups3:`Lookup`)
  WHERE (ID(result_lookups3) = {ID_result_lookups3})
  RETURN n | {:ID_result_lookups1=>1, :ID_result_lookups3=>2}

Please note that the cypher is generated with

config.neo4j.id_property = :neo_id

which however is not relevant in this issue as any other property in the where condition is experiencing the same problem.

Additional information which could be helpful if relevant to your issue:

Code example (inline, gist, or repo)

Runtime information:

Neo4j database version: neo4j gem version: neo4j-core gem version:

klobuczek avatar Oct 15 '18 18:10 klobuczek

Further insights. Even this simpler case exposes what I believe, after some analysis, is the same bug as above and fixing the below will fix as well the above.

[7] pry(main)> Role.manager.branch { manager }
=>  Role#manager#manager
  MATCH (node2:`Role`)
  MATCH (node2)-[rel1:`manager`]->(result_manager:`Role`)
  MATCH (result_manager)-[rel2:`manager`]->(result_manager:`Role`)
  RETURN result_manager

The generated query should return all managers which have their own managers, not managers which are their own manager.

klobuczek avatar Oct 24 '18 17:10 klobuczek