GAE:模型失去了父母与子女关系的追踪

时间:2011-06-05 17:23:20

标签: python database google-app-engine

我在谷歌应用引擎数据存储中遇到了一个与实体关系非常奇怪的问题。我正在研究Python / GAE webapp(学习练习),可以在sourceforge找到完整的代码。

  • 我有2个型号:
    • Gallery - 搜索字词和(间接)照片列表
    • Photo - 有关照片的信息,以及它所属的图库(collection_index ='photos')
  • 我有一个摄取过程,可以创建画廊并为他们添加照片
  • 我有一个从数据存储区读取图库的页面,并使用该Gallery实例的“.photos”属性来获取其中的照片列表

现在这就是怪异部分的来源...... 如果我更改了一个文件(我测试过的任何文件),甚至只是更新文件的时间戳(即重新加载)。 ..画廊的“.photos”属性开始失败。例如,如果我尝试加载page for the "flowers" gallery

Traceback (most recent call last):
  File "C:\Applications\Google\google_appengine\google\appengine\ext\webapp\__init__.py", line 700, in __call__ handler.get(*groups)
  File "C:\Eclipse-Workspace\galleries-google\app\views\gallery.py", line 33, in get for photo in gallery.photos:
AttributeError: 'Gallery' object has no attribute 'photos'

我可以重新启动webapp,我可以重新启动应用程序启动器,然后启动webapp。无论如何,问题仍然存在。我似乎需要强制数据存储区以某种方式“记住”连接

# Reading the list of photos for a given gallery via the Photo entity
# This seems to force the datastore to "remember" the connection between the two
from google.appengine.ext import db
import pprint
from app.models.gallery import Gallery
from app.models.photo import Photo

gallery = Gallery.get_by_key_name('candy')
print("Gallery: " + gallery.search_term)
q = db.GqlQuery("SELECT * FROM Photo WHERE gallery = :1", gallery )
photos = q.fetch(20)

for photo in photos :
    print("\tphoto: " + photo.getUrl('original'))

或者从头开始重新摄取所有数据(虽然我想甚至只需重新摄取一个图库就可以了)。

是否有人对可能导致此问题的原因有任何想法?任何帮助都将不胜感激。

注意:

  • 这是在开发环境中。我还没有达到在真实服务器上将其注册为webapp的程度。
  • 奇怪的是,我最近在GAE中询问了如何How to list child nodes in parent的问题...因为我真的希望信息存在于父模型定义中。讽刺的是(显然)父母中缺少那些导致问题的信息

1 个答案:

答案 0 :(得分:2)

所以,经过多次挖掘,我相信我找到了答案......

  • 修改文件(时间戳更改)后,应用引擎会使其编译/缓存的文件无效
  • 定义Photo类后,会向Gallery based on the collection of ids it has for them
  • based on the collection of ids it has for them
  • When it needs the Gallery class, the app engine reloads the code for it... because it's definition was invalidated by a file being modified
  • Since it doesn't know it needs the Photo class, it doesn't load that file, so the "new instance" of the Gallery class doesn't have the property that should be created for it's collection
  • {{1}添加一个属性有问题的软件包的文件包含两个文件的依赖项。因此,只要需要其中一个文件,就会加载它们。