neomodel icon indicating copy to clipboard operation
neomodel copied to clipboard

Using order_by with Relationship Properties

Open hosford42 opened this issue 8 years ago • 7 comments

I see in the documentation that I can use obj.rel.order_by('prop_name') to order the relatives by the given node property. But what if I want to order by a relation property? Something like this:

class OrderedRel(StructuredRel):
    order = IntegerProperty(required=True)

class Item(StructuredNode):
    ...

class NodeSequence(StructuredNode):
    item = RelationTo('Item', 'ITEM', model=OrderedRel)

for item in node_sequence.item.order_rel_by('order'):
    ...

hosford42 avatar Jul 07 '17 23:07 hosford42

Great idea.. yea its definitely missing.

robinedwards avatar Jul 12 '17 08:07 robinedwards

Then? Any solution?

Andy-Geng avatar Jan 03 '18 23:01 Andy-Geng

Waiting for this.

ncastrohub avatar May 09 '19 19:05 ncastrohub

Yes, this would be really helpful. Only workaround is loading all objects and then finding the according relationships with model1.relationship_name.relationship(model2). After that I can retrieve the relationship properties and sort the objects accordingly. I'm sure this can be done much faster natively inside neo4j though.

Askir avatar Jun 07 '19 11:06 Askir

Actually a second variant would be to just model the relationship as a node and add the properties to that node. Then on both sides add Relationships to the original models with cardinality 1.

That would result in: map(lambda x: x.model2 , model1.relationship.order_by('property'))

Askir avatar Jun 07 '19 12:06 Askir

I maked a class that parametrize the 'order by some relation property' and then used the cipher to make/execute the query. After that i inflated the values (the result nodes) and it worked.

ncastrohub avatar Jun 07 '19 12:06 ncastrohub

Any update on this? I was hoping to encode an "active" attribute in a relationship and use that to filter my queries. I will try the suggestions above.

zacharymostowsky avatar Oct 26 '20 02:10 zacharymostowsky