ReferenceProperty() 上的 Gql 查询

发布于 2024-11-05 06:10:38 字数 1379 浏览 0 评论 0 原文

我的问题的简短版本:基于引用属性的查询过滤的 Gql 语法是什么?

我的问题的长版本: 假设以下模型:

Class User(db.Model):
    username = db.StringProperty()
    password = db.StringProperty()

Class Portfolios(db.Model):
    portname = db.StringProperty()

Class Portfolio_Owners(db.Model):
    port_id = db.ReferenceProperty(Portfolios)
    user_id = db.ReferenceProperty(User)
    key_string = db.StringProperty()

我想根据 user_id(这是一个 ReferenceProperty)查询 Portfolio_Owners。

但是,我无法像在 StringProperty 上那样查询 .user_id 属性的字符串(以下返回一个空列表):

key_string = 'aglwb3J0Zm9saW9yCwsSBFVzZXIY6gEM'

que=db.GqlQuery("SELECT * FROM Portfolio_Owners WHERE user_id=:1", key_string)

entity = que.fetch(limit=10)

我也不能查询该用户的关键对象(以下也返回一个空列表):

key_object = db.Key('aglwb3J0Zm9saW9yCwsSBFVzZXIY6gEM')

que=db.GqlQuery("SELECT * FROM Portfolio_Owners WHERE __key__=:1", key_object)

entity = que.fetch(limit=10)

是的,我确定 user_id 是正确的...这是来自数据存储查看器的复制粘贴:

Entity Kind  Portfolio_Owners
Entity Key   aglwb3J0Zm9saW9yFwsSEFBvcnRmb2xpb19Pd25lcnMYhQIM
ID   261
user_id (Key)   aglwb3J0Zm9saW9yCwsSBFVzZXIY6gEM
User:           id=234

port_id (Key)   aglwb3J0Zm9saW9yEQsSClBvcnRmb2xpb3MYhAIM
Portfolios:     id=260 

我没有在 App Engine 文档、App Engine 消息板或其他地方找到答案。

谢谢!

Short version of my question: What is the Gql syntax for a query filtering based on a reference property?

Long version of my question:
Assume the following model:

Class User(db.Model):
    username = db.StringProperty()
    password = db.StringProperty()

Class Portfolios(db.Model):
    portname = db.StringProperty()

Class Portfolio_Owners(db.Model):
    port_id = db.ReferenceProperty(Portfolios)
    user_id = db.ReferenceProperty(User)
    key_string = db.StringProperty()

I want to query Portfolio_Owners based on user_id (which is a ReferenceProperty).

However, i can not query on a string of the .user_id property as you would on a StringProperty (the following returns an empty list):

key_string = 'aglwb3J0Zm9saW9yCwsSBFVzZXIY6gEM'

que=db.GqlQuery("SELECT * FROM Portfolio_Owners WHERE user_id=:1", key_string)

entity = que.fetch(limit=10)

Nor can i query on a key object of that user (the following also returns an empty list):

key_object = db.Key('aglwb3J0Zm9saW9yCwsSBFVzZXIY6gEM')

que=db.GqlQuery("SELECT * FROM Portfolio_Owners WHERE __key__=:1", key_object)

entity = que.fetch(limit=10)

And yes, I certain the user_id is correct...here is the copy-paste from the Datastore Viewer:

Entity Kind  Portfolio_Owners
Entity Key   aglwb3J0Zm9saW9yFwsSEFBvcnRmb2xpb19Pd25lcnMYhQIM
ID   261
user_id (Key)   aglwb3J0Zm9saW9yCwsSBFVzZXIY6gEM
User:           id=234

port_id (Key)   aglwb3J0Zm9saW9yEQsSClBvcnRmb2xpb3MYhAIM
Portfolios:     id=260 

I have not found the answer in App Engine docs, App Engine msg boards, or elsewhere on SO.

thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮色兮凉城 2024-11-12 06:10:38

那么 aglwb3J0Zm9saW9yCwsSBFVzZXIY6gEM 是您的用户密钥,对吧?让我们从这里开始:

user = db.Key('aglwb3J0Zm9saW9yCwsSBFVzZXIY6gEM')
que = db.GqlQuery("SELECT * FROM Portfolio_Owners WHERE user_id = :1", user)

这应该为您提供引用该用户的任何 Portfolio_Owners 实体。

您可以将其缩短为:

user = db.get('aglwb3J0Zm9saW9yCwsSBFVzZXIY6gEM')
que = user.Portfolio_Owners_set

数据存储从根本上与 SQL 非常不同。根据您的代码示例,我认为您可以通过重新访问文档来更好地了解数据存储区设计而受益:

http://code.google.com/appengine/docs/python/datastore/entities.html

So aglwb3J0Zm9saW9yCwsSBFVzZXIY6gEM is your user key, right? Let's start with this:

user = db.Key('aglwb3J0Zm9saW9yCwsSBFVzZXIY6gEM')
que = db.GqlQuery("SELECT * FROM Portfolio_Owners WHERE user_id = :1", user)

This should give you any Portfolio_Owners entities that refer to this user.

You can shorten that to this:

user = db.get('aglwb3J0Zm9saW9yCwsSBFVzZXIY6gEM')
que = user.Portfolio_Owners_set

The datastore is fundamentally very different from SQL. Based on your code samples, I think you would benefit from revisiting the docs to get a better understanding of datastore design:

http://code.google.com/appengine/docs/python/datastore/entities.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文