activegraph
activegraph copied to clipboard
Wrong reuse of identical node aliases in generated cypher query.
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
- Reuse of the
result_lookups
for both match conditions. - 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:
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.