question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Entity Key isn't set

See original GitHub issue

Hi! When using this library to query an SAP ByD OData service I get an empty entity: image

When I enable debugger it looks like it’s getting the data I want, just not setting the entity as expected/putting it somewhere I can access (see the last debug statement, I put ** around it):

>>> threads = byd_client.entity_sets.BusinessPartnerCollection.get_entity('00163E0DBAC41ED7AD87562C419D1A6C').execute()
... 
DEBUG:pyodata.service:Detected single property key, adding pair ObjectID->00163E0DBAC41ED7AD87562C419D1A6C to keyproperties
INFO:pyodata.service:Getting entity BusinessPartner for key 00163E0DBAC41ED7AD87562C419D1A6C and args {}
DEBUG:pyodata.service:New instance of EntityGetRequest for last segment: BusinessPartnerCollection
DEBUG:pyodata.service:Send (execute) GET request to https://my340402.sapbydesign.com/sap/byd/odata/cust/v1/accountinfo/BusinessPartnerCollection('00163E0DBAC41ED7AD87562C419D1A6C')
DEBUG:pyodata.service:  query params: {}
DEBUG:pyodata.service:  headers: {'Accept': 'application/json'}
DEBUG:urllib3.connectionpool:https://my340402.sapbydesign.com:443 "GET /sap/byd/odata/cust/v1/accountinfo/BusinessPartnerCollection('00163E0DBAC41ED7AD87562C419D1A6C') HTTP/1.1" 200 576
DEBUG:pyodata.service:Received response
DEBUG:pyodata.service:  url: https://my340402.sapbydesign.com/sap/byd/odata/cust/v1/accountinfo/BusinessPartnerCollection('00163E0DBAC41ED7AD87562C419D1A6C')
DEBUG:pyodata.service:  headers: {'content-type': 'application/json; charset=utf-8', 'content-length': '576', 'cache-control': 'no-cache, no-store', 'dataserviceversion': '2.0', 'c4c-odata-response-time': '111  ms', 'strict-transport-security': 'max-age=31536000; includeSubDomains'}
DEBUG:pyodata.service:  status code: 200
DEBUG:pyodata.service:  body: {"d":{"results":{"__metadata":{"uri":"https://my340402.sapbydesign.com/sap/byd/odata/cust/v1/accountinfo/BusinessPartnerCollection('00163E0DBAC41ED7AD87562C419D1A6C')","type":"cust.BusinessPartner"},"ObjectID":"00163E0DBAC41ED7AD87562C419D1A6C","RoleCode":"CRM000","StartDate":"\/Date(-62135769600000)\/","EndDate":"\/Date(253402214400000)\/","InternalID":"1060169","BusinessPartnerDefaultWeb":{"__deferred":{"uri":"https://my340402.sapbydesign.com/sap/byd/odata/cust/v1/accountinfo/BusinessPartnerCollection('00163E0DBAC41ED7AD87562C419D1A6C')/BusinessPartnerDefaultWeb"}}}}}
**DEBUG:pyodata.service:New entity proxy instance of type BusinessPartner from properties: {'results': {'__metadata': {'uri': "https://my340402.sapbydesign.com/sap/byd/odata/cust/v1/accountinfo/BusinessPartnerCollection('00163E0DBAC41ED7AD87562C419D1A6C')", 'type': 'cust.BusinessPartner'}, 'ObjectID': '00163E0DBAC41ED7AD87562C419D1A6C', 'RoleCode': 'CRM000', 'StartDate': '/Date(-62135769600000)/', 'EndDate': '/Date(253402214400000)/', 'InternalID': '1060169', 'BusinessPartnerDefaultWeb': {'__deferred': {'uri': "https://my340402.sapbydesign.com/sap/byd/odata/cust/v1/accountinfo/BusinessPartnerCollection('00163E0DBAC41ED7AD87562C419D1A6C')/BusinessPartnerDefaultWeb"}}}}**
INFO:pyodata.service:Initiating property request for __len__
INFO:pyodata.service:Initiating property request for __len__
INFO:pyodata.service:Initiating property request for url

As a result I seem to get an error when I try to get a property from that:

>>> threads.InternalID
INFO:pyodata.service:Initiating property request for InternalID
Traceback (most recent call last):
  File "C:\Users\dwilliams\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pyodata\v2\service.py", line 783, in __getattr__
    return self._cache[attr]
KeyError: 'InternalID'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "C:\Users\dwilliams\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pyodata\v2\service.py", line 786, in __getattr__
    value = self.get_proprty(attr).execute()
  File "C:\Users\dwilliams\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pyodata\v2\service.py", line 847, in get_proprty
    path = urljoin(self.get_path(), name)
  File "C:\Users\dwilliams\AppData\Local\Programs\Python\Python38-32\lib\site-packages\pyodata\v2\service.py", line 830, in get_path
    return self._entity_set._name + self._entity_key.to_key_string()  # pylint: disable=protected-access
AttributeError: 'NoneType' object has no attribute 'to_key_string'
INFO:pyodata.service:Initiating property request for __len__
INFO:pyodata.service:Initiating property request for __len__
INFO:pyodata.service:Initiating property request for url

If I do something like this, it sets the entity key for each entity:

>>> bp_coll = byd_client.entity_sets.BusinessPartnerCollection.get_entities().select('ObjectID,InternalID').execute()
>>> print(bp_coll[0].InternalID)
'NBS'

image

Am I doing something wrong here…?

Thanks for your time and expertise, Dan Williams

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
filak-sapcommented, Apr 3, 2020

The $metadata doesn’t seem to support JSON:

Yep, sorry about that, I forgot to mention that I need only the entity.

Thank you! Now, I will try to fix the problem.

1reaction
squeakybootscommented, Jan 19, 2021

Hi @sti0 ! I do it like this:

def byd_client():
    account_url = "https://my000000.sapbydesign.com/sap/byd/odata/cust/v1/your_service/"
    session = requests.session()
    session.auth = ('your_user', 'your_password')
    session.headers['odata-v2-strict-json-format'] = 'true'
    # session.headers['Accept'] = 'odata=minimalmetadata'

    def get_csrf(url):
        try:
            session.headers['x-csrf-token'] = 'fetch'
            r = session.get(url)
            session.headers['x-csrf-token'] = r.headers['x-csrf-token']
        except KeyError:
            print(f'Failed retrieve CSRF Token. Check your credentials and make sure the connection is up.')

    get_csrf(account_url)
    return pyodata.Client(account_url, session)

byd_client = byd_client()
Read more comments on GitHub >

github_iconTop Results From Across the Web

entity key - ADO.NET | Microsoft Learn
An entity key is a property or a set of properties of an entity ... entity key for a given entity type; surrogate...
Read more >
Not able to set RecId as Entity Key during creation...
Not able to set RecId as Entity Key because i am getting only these field in the drop down.(Please refer the image attached)....
Read more >
JPA hibernate foreign key not set in entity - Stack Overflow
1. We need a complete minimal example to understand what you're doing (transaction boundaries, etc.). 2. You shouldn't have a prentId field in ......
Read more >
The primary key fields are not set for this descriptor
Hello I am working on a new toplink project, I have a question regarding whether to set a primary key for a table?...
Read more >
How to create a custom entity for Dual-write
In today's article, I will show how you can create your own custom Dual Write integrations between Dynamics 365 Supply Chain Management and ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found