10

Google App Engine の NDB で奇妙な問題に遭遇した人はいないだろうか。新しいエンティティを作成して で保存した後put()。そしてquery()すぐに、常にアイテムが 1 つ少なくなります。例えば、

class Item(ndb.Model):
    ...
    ...

items = Item.query().fetch()
length1 = len(items)
item = Item()
item.put()
items = Item.query().fetch()
length2 = len(items)

上記では、length1は常に と等しくなりlength2ます。ただし、length2後で同じ HTML ページにアクセスしたときに修正されます。何が問題ですか?ありがとう。

4

5 に答える 5

21

これは予想される動作です。上記のクエリは結果整合性のみです。つまり、クエリを実行したときに最新の結果が得られるとは限りません。

これは、祖先クエリを使用して回避できます(上記のリンクを参照)。たとえば、各アイテムに親エンティティを指定してから、を使用する必要がありますItem.query().ancestor(theParentEntity).fetch()

于 2013-03-17T13:13:34.770 に答える
8

As @JesseRusak said, you need a Dummy ancestor to solve this little problem (I had the same problem that you recently).

But I didn't make a new DummyEntity, just only a DummyKey for a DummyAncestor. Try this for your problem:

class Item(ndb.Model): ... ... items = Item.query(ancestor=ndb.Key(Item, 'Items')).fetch() length1 = len(items) item = Item(parent=ndb.Key(Item, 'Items')) item.put() items = Item.query(ancestor=ndb.Key(Item, 'Items')).fetch() length2 = len(items)

At least in my case, the DummyAncestor worked.

于 2013-05-21T11:38:21.103 に答える
-3

適切なクエリを作成し、新しいモデル レコードを作成し、それをクエリに追加してから返すことで、これを解決しました。あなたのものを変更すると、次のようになります。

class Item(ndb.Model):
    ...
    ...

items = Item.query().fetch()
length1 = len(items)
item = Item()
item.put()

appended_items = list()
for existing_item in items:
    appended_items.append(existing_item)

appended_items.append(item)
length2 = len(appendeditems)

この場合、appended_items にはクエリと新しい要素が含まれています。リストの生成は非効率的ですが、私は python/ndb の初心者であり、コレクションをクエリ モデルから直接取得する方法がおそらくあると思います。

于 2013-11-17T20:32:28.743 に答える