neo4j-contrib/neomodel

neomodel_inspect_database - Only writes first relationship

schlopmyflop opened this issue · 2 comments

Expected Behavior

neomodel_inspect_database writes all relationships to StructuredNode

Actual Behavior

neomodel_inspect_database only writes first relationship to StructuredNode

How to Reproduce the Problem

create node with 2 different outgoing relationship types

Simple Example

create (p1:Person)
create (p2:Person)
create (p3:Pet)
create (p1)-[:HAS_FRIEND]->(p2)
create (p1)-[:HAS_PET]->(p3)

run neomodel_inspect_database script

neomodel_inspect_database --db bolt://neo4j:neo4j@localhost:7687 --write-to output.py

output:

from neomodel import StructuredNode, RelationshipTo, StructuredRel, ZeroOrOne

class Person(StructuredNode):
    has_friend = RelationshipTo("Person", "HAS_FRIEND", cardinality=ZeroOrOne)

expected output (which can be replicated by removing the limit clause in RelationshipInspector.outgoing_relationships):

from neomodel import StructuredNode, RelationshipTo, StructuredRel, ZeroOrOne

class Person(StructuredNode):
    has_friend = RelationshipTo("Person", "HAS_FRIEND", cardinality=ZeroOrOne)
    has_pet = RelationshipTo("Pet", "HAS_PET", cardinality=ZeroOrOne)

Cause

the LIMIT clause in RelationshipInspector.outgoing_relationships causes only the first relationship to be returned:

class RelationshipInspector:
@classmethod
def outgoing_relationships(cls, start_label, get_properties: bool = True):
if get_properties:
query = f"""
MATCH (n:`{start_label}`)-[r]->(m)
WITH DISTINCT type(r) as rel_type, head(labels(m)) AS target_label, keys(r) AS properties, head(collect(r)) AS sampleRel
ORDER BY size(properties) DESC
RETURN rel_type, target_label, apoc.meta.cypher.types(properties(sampleRel)) AS properties LIMIT 1
"""
else:
query = f"""
MATCH (n:`{start_label}`)-[r]->(m)
WITH DISTINCT type(r) as rel_type, head(labels(m)) AS target_label
RETURN rel_type, target_label, {{}} AS properties LIMIT 1
"""
result, _ = db.cypher_query(query)
return [(record[0], record[1], record[2]) for record in result]

Specifications

Versions

  • OS: Windows 11
  • Library: 5.2.1
  • Neo4j: 5.12.0

Hey ! Now that neomodel 5.3.0 is out and work on async is done ; I hope I can soon have a look into that !

Thank you for the nicely documented issue :)

@schlopmyflop Fixed in the attached PR, should make it into 5.3.1