0

So on my Ndb Store I have an Entity that looks like this

SampleUser<user_key="Key('User', 4653133208748032)", job_key="Key('Job', 4978588650569728)", company_key="Key('Company', 1222222222)", status="application", email="saaa.sss@gmail.com", job_id="21232", first_name="mark", last_name="chime", phone="None", department="None", ref_type="[u'employee']", referral_date="2021-10-03 07:04:08.999648", referrer="chukuwka chime <sssss.massssrk@gmail.com>", application_status="new", deleted="False", resume_uuid="14ac3e22-88d6-4fd2-97fe-e31ca853e5b3", resume_filename="resume-sample.pdf"> with key 6341983069011968

I want to update one of the key fields job_key="Key('Job', 4978588650569728)" with a new value but not sure how . this is like updating a forign_key in mysql

Here is what I tried

entity_id = "6341983069011968"
entity = ndb.Key('JobApplication', int(entity_id)).get()
def update_entity_from_key(key):
    sandy = key.get()
    sandy.job_key.id = 5673479999324164
    sandy.put()
    
update_entity_from_key(entity.key)

but I get the error

AttributeError: 'Key' object attribute 'id' is read-only
chukwuka mark
  • 154
  • 2
  • 11

1 Answers1

3

This should fix it:

def update_entity_from_key(key, job_key_id):
    sandy = key.get()
    sandy.job_key = Key('Job', job_key_id)
    sandy.put()

Your code was modifying the id of an existing key and that is not allowed. You need to create a new key.

new name
  • 15,861
  • 19
  • 68
  • 114