LoginSignup
4
2

More than 5 years have passed since last update.

GoogleAppsEngineでancestor queryを使うときはparentには実在するentityを使わなくてもよい

Last updated at Posted at 2016-06-09

タイトルで言いたいことをいいました。
ancestor queryを使うことによりStrongly consistentが適用されます。

from google.appengine.ext import ndb

class Customer(ndb.Model):
    name = ndb.StringProperty()

class Purchase(ndb.Model):
   price = ndb.IntegerProperty()

customer_entity = Customer(name='me', id='aaa@example.com')
purchase1 = Purchase(parent=customer_entity.key, price=100)
purchase2 = Purchase(parent=customer_entity.key, price=200)
purchase1.put()
purchase2.put()
print Purchase.query(ancestor=customer_entity.key).fetch()
#[Purchase(key=Key('Customer', 'aaa@example.com', 'Purchase', 5414888608366592), price=200), Purchase(key=Key('Customer', 'aaa@example.com', 'Purchase', 5977838561787904), price=100)]

key = ndb.Key('user', 'bbb@example.com')
purchase3 = Purchase(parent=key, price=100)
purchase4 = Purchase(parent=key, price=200)
purchase3.put()
purchase4.put()
print Purchase.query(ancestor=key).fetch()
#[Purchase(key=Key('user', 'bbb@example.com', 'Purchase', 4711201166589952), price=200), Purchase(key=Key('user', 'bbb@example.com', 'Purchase', 6540788515209216), price=100)]
4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2