Note that there are some explanatory texts on larger screens.

plurals
  1. POHow reliable is NDB's put()?
    text
    copied!<p>I'm having some strange behavior with my models.</p> <p>I have Event model, which keeps tracks of events. And an "Aggregate" model, which stores an aggregate of records of model A, grouped by date.</p> <pre><code>class Event(ndb.Model): user = ndb.KeyProperty(kind=UserProfile) date = ndb.DateTimeProperty(auto_now_add=True) event_type = ndb.StringProperty() is_aggregated = ndb.BooleanProperty(default=False) class Aggregate(ndb.Model): user = ndb.KeyProperty(kind=UserProfile) date = ndb.DateProperty() aggregates = ndb.PickleProperty(default={}) </code></pre> <p><strong><code>Event</code></strong> has an <code>aggregate()</code> method, like so:</p> <pre><code>def aggregate(self): if self.is_aggregated: # already aggregated return # get an existing aggregate agg = Aggregate.query( Aggregate.user == self.user, Aggregate.date == self.date.date() ).get() if not agg: # or create a new one agg = Aggregate( user=self.user, date=self.date.date() ) agg.put() # update aggregate's count if self.event_type not in agg.aggregates.keys(): agg.aggregates[self.event_type] = 0 agg.aggregates[self.event_type] += 1 agg.put() # mark this as aggregated self.is_aggregated = True self.put() </code></pre> <p>Now, in my handlers, every time a new <code>Event</code> is created, I call its <code>aggregate</code> method. But there's some discrepancy with the numbers. </p> <p>For example, I have 20 <code>Event</code> records with the <code>date</code> between <code>2013-10-10 00:00:00</code> and <code>2013-10-10 23:59:59</code> and <code>event_type = "add"</code>, all of them have <code>is_aggregated = True</code>. That would imply that the <code>aggregate</code> method executed successfully for each.</p> <p>But when I view the corresponding <code>Aggregate</code> record with the <code>date = 2013-10-10</code>, the <code>aggregates</code> property doesn't reflect 20 <code>"add"</code> events. It only has 16.</p> <p>What would cause such a discrepancy? How would <code>self.put()</code> succeed but <code>agg.put()</code> not succeed?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload