tags:

views:

140

answers:

2

I'm trying to query records in Google App engine where an IntegerProperty is null (None). This is what I tried without success:

data = db.GqlQuery("SELECT * FROM MyModel WHERE intProp=:1",None)

And also with a Query:

query = db.Query(MyModel)
query = query.filter('intProp', None) 
data = query.fetch(limit=100)

Any help would be appreciated.

class MyModel(db.Model):
    intProp = db.IntegerProperty()
+2  A: 

Your code looks correct. Do you actually have any instances of MyModel with an intProp of None...?

Edit: per the OP's comment it's apparently an issue of schema migration; he added a new property and expected existing entities to have it appear (set to None). That's now how schema changes in GAE work, per the docs:

The App Engine datastore doesn't require all entities to have the same set of properties. After updating your models to add new properties, existing entities will continue to exist without these properties. In some situations, this is fine, and you don't need to do any more work. When would you want to go back and update existing entities so they also have the new properties? One situation would be when you want to do a query based on the new properties. In our example with Pictures, queries like "Most popular" or "Least popular" wouldn't return existing pictures, because they don't (yet) have the ratings properties. To fix this, we'll need to update the existing entities in the datastore.

The essay I'm quoting continues by showing one way to do it, but it's a pretty old-fashioned approach, from before GAE had scheduled tasks, remote apis, &c. We can do it better now. App Engine Fan has a very recent post that shows the general approach (and a mistake he made during his own schema migration, so that others can avoid it!), and points to the remote API as the key tool; he also points to a module (from the Rietveld open source core-review project, initiated by Guido van Rossum, author of Python and a key contributor in the development of App Engine) that does the task neatly and properly (you'll need to tweak that code a little so it uses your models &c instead of Rietveld's, of course).

Alex Martelli
I've added intProp as a new property to the model. I have instances of MyModel, but intProp contains no data. Maybe new properties are not assigned None?
lajos
Right, when you change the schema the system doesn't intrinsically go over all existing entities, modify them, and save them back; that would potentially be very costly if you had many. You need to manage the schema migration yourself. See http://code.google.com/appengine/articles/update_schema.html .
Alex Martelli
Thanks for your help!
lajos
A: 

I managed to query entities with a NULL value using a trick like this:

SELECT * FROM MyModel WHERE intProp < 0

Naturally there is no garantie that such a nondocumented method will be compatible with future versions.