Retrieve nodes by their relationship to another node

44 Views Asked by At

This is my Neomodel class:

class Sample(StructuredNode):
    uid = UniqueIdProperty()
    name = StringProperty(unique_index=True)


class Annots(StructuredNode):
    uid = UniqueIdProperty()    
    attributedTo = RelationshipTo('Sample', 'attributedTo', OneOrMore)
    assignedBy = RelationshipFrom('User', 'assignedBy', OneOrMore)

The user selects a sample, so I need to retrive all anotations that are linked to that sample. Using Cquery is straightforward:

MATCH (a:Annots)-[r:attributedTo]->(s:Sample)
where s.name = 'x'
RETURN a

But when I run into Python Neomodel I just can't figure out how to make it other than this and it seems not the right way.

current_sample = Sample.nodes.first_or_none(name=samples[selected_sample])
if current_sample is not None:
    related_annots = Annots.nodes.all(attributedTo=current_sample)

ValueError: No such property attributedTo on Annots. Note that Neo4j internals like id or element_id are not allowed for use in this operation.

I have thought of loading all annots nodes and then iterate to check their attribute but I don't think it is the best solution. Also I can change the relationship to Sample class, but I think it is better located in Annots class.

Any thoughts here?

--------- Update ---------

I can solve it by changing the Sample class to:

class Sample(StructuredNode):
    uid = UniqueIdProperty()
    name = StringProperty(unique_index=True)
    attributedfrom = RelationshipFrom('Annots', 'attributedTo', OneOrMore)

and making this filter call:

related_annots = current_sample.attributedfrom.all()

Still not convinced this is the propoer way to get around thisproblem

0

There are 0 best solutions below