gsinha
gsinha

Reputation: 1185

Overwriting an entity by reusing its id

If we add a second entity of same Model(NDB) with the same id, would the first entity get replaced by the second entity?
Is this the right way? In future, would this cause any problem?

I use GAE Python with NDB.

Eg,

class X (ndb.Model): 
    command = ndb.StringProperty () 

x_record = X (id="id_value", command="c1") 
x_record.put () 


# After some time  
x_record = X (id="id_value", command="c2") 
x_record.put ()

I did find a mention of this in official Google docs.

CONTEXT
I intend to use it to reduce code steps. Presently, first the code checks if an entity with key X already exists. If it exists, it updates its properties. Else, it creates a new one with that key(X). New approach would be to just blindly create a new entity with key X.

Upvotes: 0

Views: 114

Answers (1)

Paul Collingwood
Paul Collingwood

Reputation: 9116

Yes, you would simply replace the model.

Would it cause any problems? Only if you wanted the original model back...

Upvotes: 3

Related Questions