yasir
yasir

Reputation: 137

how to make a key in Datastore based upon more than one property

how to make own unique key based upon more than one peoperties(fields) of a model(Kind) in datastore.

here it is a situation i have 4 fields name,device,id,data. now i want to make a key based upon my 3 fields i.e name,device,id so that whenever data comes and if key is already present against that data then it will be replaced against that key else a new data data with new key is inserted.in this way i can save a db hit(i.e i will not need that hit which is required to check that a data against a key in model is present or not it will just replace data itself if already present else insert a newone record)
so how can i make this key using my 3 fields.

Upvotes: 0

Views: 167

Answers (2)

yasir
yasir

Reputation: 137

it is working now i have made a unique key, now i pass this uniqe key to key_name which is unique(default) field in Datastore.

key_name_ = game_code+game_version+device_type+device_id

EventLogModel( **key_name** = key_name_, game_code = game_code, game_version = game_version, device_type = device_type , device_id = device_id , events=events ).put()

Upvotes: 0

Sean
Sean

Reputation: 51

At the risk of stating the obvious, the simplest answer is just to make a single key that concatenates your three fields, with some delimiter that can't appear in any of them. In Java, for example, something along the lines of:

String delimiter = ":";
String key = name + delimiter + device + delimiter + id;

Upvotes: 1

Related Questions