Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NDB: Query for unset properties?

I've had some data being gathered in production for a couple of days with, lets say, the following model:

class Tags(ndb.Model):
    dt_added = ndb.DateTimeProperty(auto_now_add=True)
    s_name   = ndb.StringProperty(required=True, indexed=True)

Imagine I now add a new property to the model:

class Foo(ndb.Model):
    is_valid = ndb.BooleanProperty(default=False)
    some_key = ndb.KeyProperty(repeated=True)

class Tags(ndb.Model):
    dt_added = ndb.DateTimeProperty(auto_now_add=True)
    name     = ndb.StringProperty(required=True, indexed=True)
    new_prop = ndb.StructuredProperty(Foo)

... and gather some more data with this new model.

So now I have a portion of data that has the property new_prop set, and another portion that does not have it set.

My question is: how to I query for the data with the new property new_prop NOT set?

I've tried:

query_tags = Tags.query(Tags.new_prop == None).fetch()

But does not seem to get the data without that property set... Any suggestions? Thanks!

like image 566
Michael Gradek Avatar asked Jul 08 '14 18:07

Michael Gradek


1 Answers

The Datastore distinguishes between an entity that does not possess a property and one that possesses the property with a null value (None).

It is not possible to query for entities that are specifically lacking a given property. One alternative is to define a fixed (modeled) property with a default value of None, then filter for entities with None as the value of that property.

like image 116
pgiecek Avatar answered Nov 19 '22 12:11

pgiecek