Simon Otter
Simon Otter

Reputation: 193

Cascade delete in Google datastore for ReferenceProperty objects

Is there a concept of having the Datastore in Google App Engine carry out cascade deletes where ReferenceProperty has been used? I understand than the Datastore in GAE is not a relational database. However, consider a simple model where blog posts can be liked by users.

class Post(db.Model):
    subject = db.StringProperty(required=True)
    content = db.TextProperty(required=True)
    created = db.DateTimeProperty(auto_now_add=True)
    created_by = db.ReferenceProperty(User, required=True,
                                      collection_name='posts')

and:

class Like(db.Model):
    post = db.ReferenceProperty(Post, required=True, collection_name='likes')
    user = db.ReferenceProperty(User, required=True, collection_name='likes')

When it comes to deleting a post, I want all "likes" to be deleted also.

def delete(self, post_key):
        """ Deletes a post from the datastore """
        db.delete(post_key)
        # TODO: Should really delete any corresponding likes
        #       and comments too (else they're be orphaned)

So, must I code these deletions of likes myself, or can GAE do it automatically?

Thanks for any assistance that anyone could provide to increase my understanding.

Upvotes: 1

Views: 435

Answers (1)

Dan Cornilescu
Dan Cornilescu

Reputation: 39824

There is no cascaded/recursive delete in the datastore, you have to implement it yourself.

These might help (same goal, different reason):

Upvotes: 1

Related Questions