Skip to main content
Seasoned ⭐️⭐️
October 16, 2024

How to access reverse direct relations through sdk

  • October 16, 2024
  • 19 replies
  • 437 views

Hello team, 

I am trying to get the details of a reverse direct relation from my data model. In the SDK documentation I found some sdk methods. I have tried client.data_modeling.views.ReverseDirectRelation(source= "Entity", through="entityType") but I am getting the error that ReverseDirectRelation is not found in views could you please tell me what SDK method I should use to retrieve information on ReverseDirectRelations from my models.

19 replies

Anders  Albert
Expert ⭐️⭐️⭐️⭐️
Expert ⭐️⭐️⭐️⭐️
October 16, 2024

Hi @Gargi Bhosale ,

You cannot get the reverse direct relations using the .retrieve or .list methods, you have to use the .query method. Typically, you do have to use the `NodeResultExpression` with `node.through` set to the direct relation property of the view on the other side of the reverse direct relations and the `nodes.direction=’inwards’`. 

An alternative is to use `pygen`. `pygen` generates a tailor made SDK for your data model. In the generated, `.list` method there is a parameter `retrieve_connections` that you can use to retrieve connections. This will do the `.list` or `.query` call for you. See https://cognite-pygen.readthedocs-hosted.com/en/latest/usage/listing_filtering_retrieving.html#reading-instances-list-and-retrieve

Seasoned ⭐️⭐️
October 17, 2024

I see that pygen is highly specific on a particular model. In my case ,I intend to make the code generic to any model the user would select. In that case, would you recommend using the .query method? Are there any performance differences between the both?

Anders  Albert
Expert ⭐️⭐️⭐️⭐️
Expert ⭐️⭐️⭐️⭐️
October 21, 2024

There are two cases.

  1. If the direct relation of the other side is a single direct relation, use the query method. client.data_modeling.instances.query(...) You can see an example in the pygen example here.
  2. If the direct relation of the other side is a list of direct relations, use the search method.
    client.data_modeling.instances.search(...). You will do a lookup on the nodes with the direct relation filtering for matching the nodes with the reverse direct relation.
     
Seasoned ⭐️⭐️
October 23, 2024

The pygen example for client.data_modeling.instances.query(...) is not very clear and what I am trying is not working.

this is what I am trying can you please correct?

 

from cognite.client.data_classes.data_modeling.query import Query, Select, NodeResultSetExpression, EdgeResultSetExpression, SourceSelector
view=ViewId(space='slb-pdm-dm-governed', external_id='Property', version='1_7')
nested_prop_view_id = ViewId(space='slb-pdm-dm-governed', external_id='PropertyType', version='1_6')
properties = ["externalId", "propertyType"]
nested_prop_view_properties=["externalId"]

query= Query(
with_={"PropertyType":NodeResultSetExpression(
from_="Property",
direction="inwards",
filter= HasData(views=[nested_prop_view_id]),
through=nested_prop_view_id.as_property_ref("directSingle"),
)},
select={"PropertyType": Select([SourceSelector(nested_prop_view_id, nested_prop_view_properties)])}
)
res1 = client.data_modeling.instances.query(query)

 

Seasoned ⭐️⭐️
October 24, 2024

Another important observation: 

when I use client.data_modeling.instances.search(...) without the query because I want everything and with limit= None, I get only 100 records, whereas I have much more than them. When I increase the limit to 1000 it gives 1000 records, I have more than 1000 as well. Ideally, I expect all the data to come when I pass limit=None.

 

Anders  Albert
Expert ⭐️⭐️⭐️⭐️
Expert ⭐️⭐️⭐️⭐️
October 28, 2024

Give me a few days, and I will create a full example in the SDK docs on how to do this.

Seasoned ⭐️⭐️
November 6, 2024

Sure Anders, let me know when it is available

Anders  Albert
Expert ⭐️⭐️⭐️⭐️
Expert ⭐️⭐️⭐️⭐️
November 9, 2024
Seasoned ⭐️⭐️
November 11, 2024

Thank you, so when I use the search() for reverse direct relation of listable relation, I see that we get 1000 instances by using client.data_modeling.instances.search(activity_view, filter=is_equipment, limit=1_000)

I am not getting any cursor or any other way to retrieve the full data, all the instances satisfied with the query, can you please help me with this?

Schlumberger
November 15, 2024

@Anders Albert  - We still are not able to get the complete data with the temporarily approach or query suggested ? Do you see any code change is required in sdk or some other way of querying is required.

We are blocked as of now due to this.

Appreciate if you can expedite your response here.

Regards,
Neeraj B