سؤال

I am using the Python SDK for Google App Engine, and bottle.

Running something simple like this to set up some entities to work with:

@bottle.route('/test')
def testing():
    Statistic(team_id = 2, qty_customers = 600).put()
    Statistic(team_id = 3, qty_customers = 5).put()
    return "Done."

Statistic looks like this:

class Statistic(ndb.Model):
    team_id = ndb.IntegerProperty()
    qty_customers = ndb.IntegerProperty()

I would expect it to create one entity for each of those, and add them to the datastore. It creates two copies of each and adds them to the datastore.

How can I make the code only add one entity?

Why is it creating two?

Edit: To clarify, I am running the testing() function once, and it creates 4 entities. Two duplicates of each.

هل كانت مفيدة؟

المحلول

In the old DB (db.Model), you would have to specify key_name when creating an instance.

In the new DB (ndb.Model), you need to specify id, otherwise an incremented integer is chosen.

So calling function testing twice yields four different Statistic instances with IDs 1, 2, 3 and 4.

If you explicitly specify a different ID for each Statistic instance, then you will get only two instances.


I believe that in your case, you might as well remove the team_id field:

class Statistic(ndb.Model):
    qty_customers = ndb.IntegerProperty()

Then, specify a unique ID for every Statistic instance that you create:

def testing():
    Statistic(id = 2, qty_customers = 600).put()
    Statistic(id = 3, qty_customers = 5).put()

BTW, I think that it is recommended to use string IDs rather than integer IDs:

def testing():
    Statistic(id = '2', qty_customers = 600).put()
    Statistic(id = '3', qty_customers = 5).put()

UPDATE:

Even if you're calling function testing in your application only once, GAE often creates and destroys the application instance itself. So each time a new instance of your application is created, a new pair of Statistic instances are also created. If these instances are not already in the database, then they are added to it when you call function put (and that's why you need to specify an ID for each instance).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top