使用“IN”构建 GQL 查询参考属性
class Message(db.Model):
user = db.ReferenceProperty(User, required=True, collection_name='Message_set')
text = db.TextProperty(required=True)
我尝试了这个,但我在 gql 语句上收到 BadValueError 。
users = []
users.append(userA)
users.append(userB)
users.append(userC)
messages = Message.gql('Where user In :users', users=users).fetch(100)
class Message(db.Model):
user = db.ReferenceProperty(User, required=True, collection_name='Message_set')
text = db.TextProperty(required=True)
I tried this but I'm getting BadValueError on the gql statement.
users = []
users.append(userA)
users.append(userB)
users.append(userC)
messages = Message.gql('Where user In :users', users=users).fetch(100)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果没有完整的堆栈跟踪,很难判断(请?),但我猜 IN 不喜欢传递实体而不是键。 尝试这个:
It's hard to tell without the complete stacktrace (please?), but I would guess that IN doesn't like being passed entities rather than keys. Try this:
GQL 中的
IN
运算符实际上是由应用程序级 Python 代码合成的,作为一个循环执行一系列=
获取并将它们连接起来 - 它实际上并不是底层存储的一部分不幸的是,它的功能受到一些限制(而且它的性能从来都不是特别好)。 我不确定将它包含在 Python API 中是否是一个伟大的设计决策(我相信它在 Java API 中没有重复),但是,你就是这样。 (顺便说一句,!=
运算符也有类似的问题)。我不确定为什么这会干扰您的预期用途,但我怀疑这是由于“查询”对象是 datastore.MultiQuery 而不是普通 datastore.Query 的实例 - 如果您在自己的情况下这样做会发生什么应用程序级Python 代码 GAE 提供的应用程序级Python 代码通常做什么? 请参阅您的 SDK 源文件 google/appengine/api/datastore.py,特别是 MultiQuery 类 - 最后它归结为执行所有单独的查询并合并其结果(如果需要,按排序顺序,但这并不'这里似乎是这种情况)。
The
IN
operator in GQL is actually synthesized by application-level Python code as a loop doing a series of=
fetches and concatenating them -- it's not really part of the underlying store's capabilities and so is unfortunately subject to some limitations (and its performance is never particularly good). I'm not sure it was a great design decision to include it in the Python API (I believe it was not duplicated in the Java API), but, there you are. (Btw, the!=
operator has similar issues, too).I'm not sure why in particular this should interfere with your intended usage, but I suspect it's due to the "query" object being an instance of datastore.MultiQuery rather than plain datastore.Query -- what happens if you do in your own application-level Python code what the GAE-supplied application level Python code normally does? See in your SDK sources file google/appengine/api/datastore.py, specifically the MultiQuery class -- in the end it boils down to doing all the separate queries and merging their results (in sorted order, if required, but that doesn't seem to be the case here).
使用此模型:
此代码有效:
我在此看到了 awnser Google 群组帖子
with this models:
This code works:
I saw the awnser in this google groups post
我想您可能需要用户密钥的列表? 尝试 users.append(userA.key())。 用户引用属性的值是用户实体的关键。
I think you may want a list of users' keys? try users.append(userA.key()). The value of the user referenceproperty is a key to a user entity.