views:

197

answers:

2

Hello i am doing a very small application in google appengine and i use python. My problem is that i have two tables using de db.model ("clients" and "requests"). The table "client" has got the email and name fields and the table "requests" has got the email and issue fields. I want to do a query that returns for each request the email, issue and client name, if the email is the same in the two tables. Can anyone help, please?

+3  A: 

The app engine datastore does not support joins, so you will not be able to solve this problem with GQL. You can use two gets, one for client and one for request, or you can use a ReferenceProperty to establish a relationship between the two entities.

If you need to model a one-to-many relationship, you can do it with a reference property. For your case, it would look something like this:

class Client(db.Model):
    email = db.UserProperty()
    name = db.StringProperty()

class Request(db.Model):
    client = db.ReferencePrpoerty(Client, collection_name='requests')
    issue = db.StringProperty()

Any Client entity that has a Request associated with it will automatically get a property called requests which is a Query object that will return all Request entities that have a client field set to the particular Client entity you are dealing with.

You might also want to make sure that the code that creates Request entities set each new entity to have the Client entity for the particular user as its ancestor. Keeping these associated items in the same entity group could be helpful for performance reasons and transactions.

Adam Crossland
Hello. Thanks.The client uses the gmail email account to login in the application. One client can do many requests. As an administrator I want to see a list with all requests from all clients with the fields id and issue from table requests and client name from table clients...
jose alves
Google says:obj1, with ref_property can have (many of) obj2(which is the referenced obj)
daemonfire300
A: 

using this models:

class Client(db.Model):
    email = db.StringProperty()
    name = db.StringProperty()

class Request(db.Model):
    client = db.ReferenceProperty(Client, collection_name='requests')
    issue = db.StringProperty()    

With this code can query the data

from modelos import Client,Request

ctes=Client.all().filter("email =","[email protected]")

for ct in ctes:
   allRequest4ThisUser=Request.all().filter("client =",ct)
   for req in allRequest4ThisUser:
     print req.issue
Kristian Damian