Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change|Assign parent for the Model instance on Google App Engine Datastore

Is it possible to change or assign new parent to the Model instance that already in datastore? For example I need something like this

task = db.get(db.Key(task_key))
project = db.get(db.Key(project_key))
task.parent = project
task.put()

but it doesn't works this way because task.parent is built-in method. I was thinking about creating a new Key instance for the task but there is no way to change key as well.

Any thoughts?

like image 505
Vladimir Prudnikov Avatar asked Jun 05 '10 11:06

Vladimir Prudnikov


1 Answers

According to the docs, no:

The parent of an entity is defined when the entity is created, and cannot be changed later.

...

The complete key of an entity, including the path, the kind and the name or numeric ID, is unique and specific to that entity. The complete key is assigned when the entity is created in the datastore, and none of its parts can change.

Setting a parent entity is useful when you need to manipulate the parent and child in the same transaction. Otherwise, you're just limiting performance by forcing them both to be in the same entity group, and restricting your ability to update the relationship after the entity has been created.

Use a ReferenceProperty instead.

like image 86
Drew Sears Avatar answered Sep 20 '22 02:09

Drew Sears