Search code examples
google-cloud-datastoreapp-engine-ndb

Inequality query not working ndb


Session.sessionType is a StringProperty(repeated=True)

When I do this query:

sessions_no_type = Session.query(Session.sessionType == request.sessionType)

everything seems to be working and I get only those sessions who have request.sessionType in them

but when I try:

sessions_no_type = Session.query(Session.sessionType != request.sessionType)

I get back all sessions (regardless of request.sessionType)

What might be the problem for this? Even docs have a similar example but it does not work for me.

The same goes when trying this version:

filter = ndb.query.FilterNode("sessionType", "!=", request.sessionType)

when tested for equality it returns everything correctly, but inequality case ONLY eliminates Session objects which have ONE sessionType which is equal to the passed(request) sessionType


Solution

  • The documentation for The != and IN Operations says:

    The != (not-equal) and IN (membership) operations are implemented by combining other filters using the OR operation. The first of these,

    property != value

    is implemented as

    (property < value) OR (property > value)

    Because you are using a repeated StringProperty, if any of the strings in sessionType are not equal to request.sessionType then that entity will be returned in the query.