GAE python NDB 投影查询在开发中工作但不在生产中 [英] GAE python NDB projection query working in development but not in production

查看:27
本文介绍了GAE python NDB 投影查询在开发中工作但不在生产中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在撞墙,因为我的 Google App Engine python 项目有一个非常简单的 NDB 投影查询,它在我的本地机器上运行良好,但在部署到生产时神秘地失败了.

I've been hitting my head against the wall because my Google App Engine python project has a very simple NDB projection query which works fine on my local machine, but mysteriously fails when deployed to production.

更神秘……作为测试,我在另一个属性上添加了相同的投影,它在开发和生产中都有效!请问有人可以帮忙吗?!以下是更多详细信息:

Adding to the mystery... as a test I added an identical projection on another property, and it works in both dev and production! Could anyone help please?! Here are more details:

我有以下代表费用的实体:

I have the following entity that represents an expense:

class Entry(ndb.Model):
    datetime = ndb.DateTimeProperty(indexed=True, required=True)
    amount = ndb.IntegerProperty(indexed=False, required=True)
    payee = ndb.StringProperty(indexed=True, required=True) 
    comment = ndb.StringProperty(indexed=False)
    # ...

稍后在代码中,我正在对 Entry.payee 进行投影(以获取所有收款人的列表).作为测试,我还在 Entry.datetime 上添加了一个投影:

Later on in the code I am doing a projection on Entry.payee (to get a list of all payees). As a test I also added a projection on Entry.datetime:

log_msg = '' # For passing debug info to the browser

payeeObjects = Entry.query(ancestor=exp_traq_key(exp_traq_name), projection=[Entry.payee]).fetch()
payees = []
for obj in payeeObjects:
    payees.append(obj.payee)
log_msg += '%d payees: %s' % (len(payees), str(payees))

log_msg += ' ------------------- ' # a visual separator

dtObjects = Entry.query(ancestor=exp_traq_key(exp_traq_name), projection=[Entry.datetime]).fetch()
dts = []
for obj in dtObjects:
    dts.append(obj.datetime)
log_msg += '%d datetimes: %s' % (len(dts), str(dts))

#...other code, including passing log_msg down to the client

这是开发环境中的输出(注意控制台中显示了收款人列表和日期时间列表):

Here's the output in dev environment (notice a list of payees and a list of datetimes are displayed in console):

这是部署到应用引擎时的输出.我无法让它返回收款人列表.它一直返回一个空列表,即使在开发中它返回列表很好:

And here's the output when deployed to app engine. I can't get it to return a list of payees. It keeps returning an empty list even though in dev it returns the list fine:

我已确保在 GAE 上正确设置了索引:

I've ensured that I have the indexes properly set up on GAE:

请帮忙!

2018-12-05 更新:我在生产中添加了更多条目,他们被选中了!见截图.但是较旧的条目仍然没有返回.

2018-12-05 Update: I added a couple more entries in production and they got picked up! See screenshot. But the older entries are still not being returned.

我的直接反应是数据存储索引需要以某种方式刷新",以便它可以看到"旧条目.但问题是我昨天删除并重新创建了索引,这意味着它应该有旧条目......所以仍然需要帮助解决这个谜团!

My immediate reaction is that the datastore index needs to be "refreshed" somehow so it can "see" old entries. BUT the thing is I removed and recreated the index yesterday, which means it should have old entries... So still need help resolving this mystery!

推荐答案

我想通了.该死的它根本不直观.我希望 GAE 文档在这一点上做得更好...

I figured it out. Darn it wasn't intuitive at all. I wish GAE documentation was better on this point...

我在生产中的数据存储包含许多以前创建的条目.作为我尝试对 Entry.payee 进行投影的最新代码的一部分,我不得不将 Entry.payee 的定义从未编入索引更改为编入索引,如下所示:

My datastore in production contains a lot of previously created entries. As part of my latest code where I'm trying to do the projection on Entry.payee, I had to change the definition of Entry.payee from unindexed to indexed, like so:

payee = ndb.StringProperty(indexed=True, required=True) # Originally was indexed=False

所以现在所有位于数据存储区的条目都被投影查询忽略,因为收款人的索引忽略了这些条目.

So now all those entries sitting in the datastore are being ignored by the projection query because the index on payee ignores those entries.

所以我现在需要做的是以某种方式将所有这些旧实体迁移到 indexed=True.

So what I need to do now is somehow migrate all those old entities to be indexed=True.

更新 - 这是我进行此迁移的方式.结果比预期的要简单.

Update - here's how I did this migration. Turned out simpler than expected.

def runPayeeTypeMigration(exp_traq_name):
  Entry.query(ancestor=exp_traq_key(exp_traq_name)).fetch()
  for entry in entries:
    entry.put()

这是通过将所有条目读入更新后的数据结构(Entry.payee 被索引为 True 的数据结构)并将其写回数据存储区来实现的,这样实体现在将被索引.

This works by reading all entries into the updated datastructure (the one where Entry.payee is indexed=True) and writes it back to the datastore, so that the entity will now be indexed.

这篇关于GAE python NDB 投影查询在开发中工作但不在生产中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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