Yasser
Yasser

Reputation: 1808

Query filter not working, but GQL is: bug in app engine?

Can not reproduce this error on local devserver (1.5.0) but when we deploy code to google, it starts popping up? Is it a bug in the current online version of GAE?

Here is the problem. If we first do this:

proj = Project(created_by=users.User(email='[email protected]'))
proj.put()
user = users.get_current_user()
print user.email()  #this prints [email protected] (actual email not used)

This fetches 0 results:

projs = Project.all().filter('created_by', user).fetch(500)

But this fetches the result that should actually be fetched:

projs = GqlQuery("SELECT * FROM Project where created_by = USER('%s')"%user.email()).fetch(500)

Any idea whats going on here?

Upvotes: 1

Views: 492

Answers (3)

Nick Johnson
Nick Johnson

Reputation: 101149

This occurs because the User object you created has a matching email address, but not a matching user ID (since it doesn't have one), and thus doesn't show up as a match in query results. There are several problems with your current approach:

  • Email addresses aren't guaranteed to remain the same - a user can change their email address
  • Email addresses aren't guaranteed to be unique - someone could claim an address another user has vacated
  • Querying on user properties matches on properties other than the email address

In general, querying on the user property is dangerous and likely to lead to issues like this. I'd recommend putting the user's user_id property in a string, and querying on that, instead.

Upvotes: 3

Abdul Kader
Abdul Kader

Reputation: 5842

As i could see you are creating a user object and persisting that into created_by. i.e

users.User(email='[email protected]')

here you are creating a users.User object with email '[email protected]'. You are persisting the same into Projects' created_by. Where as you are comparing that with users.get_current_user() which gets the current logged in user object.That's why it's not fetching any object. Whereas in gql you are again creating a User object with the passed email which obviously will match the persisted user object in project.

i would say you should do something like this

proj = Project()
proj.created_by  = users.get_current_user()
proj.put()
user = users.get_current_user()
Project().all().filter('created_by',user).get()

Upvotes: 3

mgiuca
mgiuca

Reputation: 21357

The filter syntax is incorrect. You need to show the operator (in this case, '=').

projs = Project.all().filter('created_by =', user).fetch(500)

Upvotes: 0

Related Questions