Skip to main content

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.

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


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?


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.
     

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=s"externalId"]

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

 


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.

 


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


Reply