Skip to content Skip to sidebar Skip to footer

Gql With Two Tables

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

Solution 1:

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.

Solution 2:

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 =","somemail@mailbox.com.mx")

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

Post a Comment for "Gql With Two Tables"