问题描述
我一直在碰壁,因为我的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):
这是部署到应用程序引擎时的输出.我无法返回收款人清单.即使在dev中,它也会返回一个空列表,即使它可以很好地返回列表:
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投影查询可在开发中使用,但不能在生产中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!