google app engine - in gql (appengine), how do i check if an entity exists matching 2 filters before inserting a new one? -


the 2 filtered fields unique index in sql want see if entity exists based on these 2 fields before inserting new one.

currently have:

t2get = db.gqlquery("select __key__ talk2 ccc = :1 , ms = :2", c, theday) x in t2get:     thekey = x[0] if thekey:     t2 = talk2.get(thekey) else:     t2 = talk2() 

which errors with:

unboundlocalerror: local variable 'thekey' referenced before assignment 

if entity doens't exist.

any ideas?

if 2 fields unique index, maybe should instead use them key_name. faster , can use transaction, if needed.

def txn():   key_name = "%d.%d." % (c, theday)   t2 = talk2.get_by_key_name(key_name)   if not t2:     t2 = talk2(key_name=key_name)     t2.put()  db.run_in_transaction(txn) 

Comments

Popular posts from this blog

unicode - Are email addresses allowed to contain non-alphanumeric characters? -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

c++ - Convert big endian to little endian when reading from a binary file -