GQL与两个表 [英] GQL with two tables

查看:132
本文介绍了GQL与两个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我在google appengine中做了一个非常小的应用程序,我使用python。
我的问题是我有两个表使用de db.model(客户端和请求)。表客户端已经获得了电子邮件和名称字段,表请求已经获得了电子邮件和问题字段。如果两个表中的电子邮件相同,我想要为每个请求返回电子邮件,问题和客户端名称的查询。任何人都可以帮助吗?

解决方案

应用引擎数据存储不支持连接,因此您将无法解决此问题与GQL。您可以使用两个get,一个用于客户端,另一个用于请求,或者您可以使用ReferenceProperty建立两个实体之间的关系。



如果需要建模一对多关系,您可以使用引用属性来实现。对于你的情况,它看起来像这样:

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

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

任何具有与之关联的请求的客户端实体将自动获取一个名为请求的属性,该属性是一个Query对象,它将把具有客户端字段的所有请求实体返回给您正在处理的特定客户端实体。 >

您可能还需要确保创建请求实体的代码将每个新实体设置为将特定用户的客户端实体作为其祖先。将这些关联的项目保留在同一实体组中可能对性能原因和交易有帮助。


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?

解决方案

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.

这篇关于GQL与两个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆