views:

25

answers:

1

Hi Folks,

I'm exploring appengine (java), and as per subject, how do I, using DatastoreService, get an Entity based on Key and conditions?

In my scenario, Trainer's have many to many relationship with User's, so I have my structure as so

Trainer(id, name, type, department)
User(id, name, address, is_activated)
TrainerUser(id, trainer_id, user_id)

Now to get all User under a particular Trainer, I'd fetch all the user_id from TrainerUser filtering by trainer_id. All's ok. Then I want to get all activated User under the Trainer, so my plan is to loop over the fetched user_id's and call something like

Query q = new Entity('User');
q.addFilter('Key', EQUAL, userId);
q.addFilter('is_activated', EQUAL, True);

But as far as I know, Key is not a real physical property in which you can access using addFilter(), so the code at the top will just return me an empty Entity.

Is there a way to reference Key in the Entity? Any magic keyword for that?

A: 

Why not filter by trainer and is_activated? Here is Python-like pseudocode:

activated_users_for_trainer = User.all().filter("trainer =", trainer_key).filter("is_actived =", True).fetch(100)
mahmoud
This works well for one to many relationship, however in my case, since Trainer and User is on many to many relationship, the trainer_key is not in the User table.
andreas
Since you will be doing this query quite often, I'd suggest adding trainer keys to a db.ListProperty(type=db.Key) on users. This limits the number of trainers a user has to a maximum (either 500 or 1000), which should be enough.A trainer will probably have more users than a user will have trainers. Hence the list is on the User model rather than on Trainer.
mahmoud