ss7389
ss7389

Reputation: 85

Where clause in Google App Engine Datastore

The model for my Resource class is as follows:

class Resource(ndb.Model):
    name = ndb.StringProperty()
    availability = ndb.StructuredProperty(Availability, repeated=True)
    tags = ndb.StringProperty(repeated=True)
    owner = ndb.StringProperty()
    id = ndb.StringProperty(indexed=True, required=True)
    lastReservedTime = ndb.DateTimeProperty(auto_now_add=False)
    startString = ndb.StringProperty()
    endString = ndb.StringProperty()

I want to extract records where the owner is equal to a certain string. I have tried the below query. It does not give an error but does not return any result either.

Resource.query(Resource.owner== '[email protected]').fetch()

As per my understanding if a column has duplicate values it shouldn't be indexed and that is why owner is not indexed. Please correct me if I am wrong.

Can someone help me figure out how to achieve a where clause kind of functionality?

Any help is appreciated! Thanks!

Upvotes: 0

Views: 93

Answers (1)

Gwyn Howell
Gwyn Howell

Reputation: 5424

Just tried this. It worked first time. Either you have no Resource entities with an owner of "[email protected]", or the owner property was not indexed when the entities were put (which can happen if you had indexed=False at the time the entities were put).

My test:

Resource(id='1', owner='[email protected]').put()
Resource(id='2', owner='[email protected]').put()
resources = Resource.query(Resource.owner == '[email protected]').fetch()
assert len(resources) == 2

Also, your comment:

As per my understanding if a column has duplicate values it shouldn't be indexed and that is why owner is not indexed. Please correct me if I am wrong.

Your wrong!

Firstly, there is no concept of a 'column' in a datastore model, so I will I assume you mean 'Property'.

Next, to clarify what you mean by "if a column property has duplicate values":

  • I assume you mean 'multiple entities created from the same model with the same value for a specific property', in your case 'owner'. This has no effect on indexing, each entity will be indexed as expected.
  • Or maybe you mean 'a single entity with a property that allows multiple values (ie a list)', which also does not prevent indexing. In this case, the entity will be indexed multiple times, once for each item in the list.

To further elaborate, most properties (ie ones that accept primitive types such as string, int, float etc) are indexed automatically, unless you add the attribute indexed=False to the Property constructor. In fact, the only time you really need to worry about indexing is when you need to perform more complex queries, which involve querying against more that 1 property (and even then, by default, the app engine dev server will auto create the indexes for you in your local index.yaml file), or using inequality filters.

Please read the docs for more detail.

Hope this helps!

Upvotes: 2

Related Questions