GQL 无法识别过滤器中的引用属性
我有一个数据存储,其中包含约 850 个组和约 19,000 个项目。每个项目可能只属于一个组,我的应用程序中有两个模型,分别代表一个组和一个项目:
class Group(db.Model):
Id = db.IntegerProperty()
Name = db.StringProperty()
# ... some other properties
class Item(db.Model):
Id = db.IntegerProperty()
Name = db.StringProperty()
Group = db.ReferenceProperty(Group, collection_name="groupItems")
# ... some other properties
我可以使用数据存储管理来查看特定项目(即 WHERE Id = 34)并查看它是否正确连接到组 -
SELECT * FROM Item WHERE Id = 34
这为我提供了一个具有以下属性的组:
Decoded entity key: Group: id=10321
Entity key: agtzfmV2ZS1taW5lcnIMCxIFR3JvdXAY0VAM
Id: 18
如果我更改 GQL 查询以检索该组的所有项目,我将不会得到任何结果! -
SELECT * FROM Item WHERE Group = KEY('agtzfmV2ZS1taW5lcnIMCxIFR3JvdXAY0VAM') -- No Results
SELECT * FROM Item WHERE Group = KEY('Group', 'agtzfmV2ZS1taW5lcnIMCxIFR3JvdXAY0VAM') -- No Results
如果我只检索组,它会按预期工作 -
SELECT * FROM Group WHERE __key__ = KEY('agtzfmV2ZS1taW5lcnIMCxIFR3JvdXAY0VAM') -- Returns 1 Group
这同样适用于我的 Python 代码。调用:
group = Group.gql("WHERE Id = :1", 18).get()
items = Item.gql("WHERE Group = :1", group).fetch(50)
结果是一个不包含任何项目的列表。同样,
group.groupItems.fetch(500) -- Returns no results
我的问题是——我在做一些特别愚蠢的事情吗?我创建了一个具有类似结构的虚拟项目,以向自己证明这不是命名问题(即 Group 不是保留字)并且返回得很好。 (如有兴趣,请附上)。
我做错了什么?
编辑1:
根据要求,这是创建代码。 Reader 是一个 csv 阅读器(使用内置的 csv 库),它打开存储在 BLOB 存储中的 CSV。出于所有意图和目的,它只是解析 CSV 文件。如上所述。在通过我的仪表板的数据查看器中,我的项目正确绑定到一个组(一个组与项目旁边列出,我可以单击它的链接来查看该组),但是当将组作为过滤器的一部分传递给项目时,没有结果被退回 -
组 -
reader = UploaderBase().open_from_blobstore(Settings().get_group_csv_key())
upNum = 0
groupsToPut = []
for row in reader:
group = Group(Id=int(row[0]))
group.Name = row[2]
groupsToPut.append(group)
db.put(groupsToPut)
项目 -
groupCache = {}
for group in Group.all().fetch(1000):
groupCache[group.Id] = group
logging.info("Cached %d group entries locally" % len(groupCache))
items = []
reader = UploaderBase().open_from_blobstore(Settings().get_items_csv_key())
upNum = 0
for row in reader:
logging.debug("Adding row %d" % upNum)
item = Item(Id=int(row[0]))
if not row[1] is None and row[1] != "":
item.Group = groupCache[int(row[1])]
item.Name = row[2]
items.append(item)
db.put(items)
I have a datastore thats has ~850 groups and ~19,000 items. Each item may belong to only one group, I have two models in my app that represent a Group and an Item:
class Group(db.Model):
Id = db.IntegerProperty()
Name = db.StringProperty()
# ... some other properties
class Item(db.Model):
Id = db.IntegerProperty()
Name = db.StringProperty()
Group = db.ReferenceProperty(Group, collection_name="groupItems")
# ... some other properties
I can use the datastore admin to view a specific item (i.e. WHERE Id = 34) and see that it is connected correctly to a Group -
SELECT * FROM Item WHERE Id = 34
This gives me a group with the following properties:
Decoded entity key: Group: id=10321
Entity key: agtzfmV2ZS1taW5lcnIMCxIFR3JvdXAY0VAM
Id: 18
If I alter my GQL query to retrieve all items for this Group I get no results! -
SELECT * FROM Item WHERE Group = KEY('agtzfmV2ZS1taW5lcnIMCxIFR3JvdXAY0VAM') -- No Results
SELECT * FROM Item WHERE Group = KEY('Group', 'agtzfmV2ZS1taW5lcnIMCxIFR3JvdXAY0VAM') -- No Results
If I retrieve just the group, it works as expected -
SELECT * FROM Group WHERE __key__ = KEY('agtzfmV2ZS1taW5lcnIMCxIFR3JvdXAY0VAM') -- Returns 1 Group
This equally applies in my Python code. Calling:
group = Group.gql("WHERE Id = :1", 18).get()
items = Item.gql("WHERE Group = :1", group).fetch(50)
results in a list containing no items. Similarly
group.groupItems.fetch(500) -- Returns no results
My question is - am I doing something particularly stupid? I have created a dummy project with a similar structure to prove to myself that it wasnt a naming problem (i.e. that Group wasn't a reserved word) and that returns just fine. (Attached if anyone is interested).
What am I doing wrong?
EDIT 1 :
As requested, here's the creation code. Reader is a csv reader (using the inbuilt csv library) which opens a CSV stored in the BLOB store. For all intents and purposes its merely parsing a CSV file. As mentioned above. In the data viewer through my dashboard my Item is bound correctly to a group (a group is listed alongside the item and I can click through it's link to view the group) however when passing the group as part of a filter to the Item no results are returned -
Group -
reader = UploaderBase().open_from_blobstore(Settings().get_group_csv_key())
upNum = 0
groupsToPut = []
for row in reader:
group = Group(Id=int(row[0]))
group.Name = row[2]
groupsToPut.append(group)
db.put(groupsToPut)
Item -
groupCache = {}
for group in Group.all().fetch(1000):
groupCache[group.Id] = group
logging.info("Cached %d group entries locally" % len(groupCache))
items = []
reader = UploaderBase().open_from_blobstore(Settings().get_items_csv_key())
upNum = 0
for row in reader:
logging.debug("Adding row %d" % upNum)
item = Item(Id=int(row[0]))
if not row[1] is None and row[1] != "":
item.Group = groupCache[int(row[1])]
item.Name = row[2]
items.append(item)
db.put(items)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最可能的解释是,当您插入数据时,引用属性被设置为
indexed=False
。对模型的更改仅影响模型更改后写入的实体,因此在为该列禁用索引时插入的行将不会有索引行。The most likely explanation is that when you inserted the data, the reference property was set as
indexed=False
. Changes to a model only affect entities written after the model was changed, so rows inserted while indexing was disabled for that column will not have index rows.