Web2Py/Google App Engine Apps - How to get ID or key after Inserting New Rec

239 views
Skip to first unread message

BrendanC

unread,
Feb 20, 2012, 4:47:45 PM2/20/12
to web...@googlegroups.com
I want to build a mapping/xref/intersect  table that needs to be updated after a new record insert. For that I need the key or id of the last inserted record.
Is this available with the DAL/GAE? Elsewhere I have seen an issue where this did not work with a bulk insert.

Unfortunately, this is not covered in the docs (AFAIK).

BrendanC 

Anthony

unread,
Feb 20, 2012, 4:57:27 PM2/20/12
to web...@googlegroups.com
How are you inserting the record? The .insert() method returns the id of the inserted record, and .bulk_insert() returns a list of id's.

BrendanC

unread,
Feb 20, 2012, 8:09:53 PM2/20/12
to web...@googlegroups.com
Just did some more testing -    insert works OK, but bulk insert fails. FWIW I think this was previously reported as a problem. Here;s a link:

howesc

unread,
Feb 21, 2012, 7:57:15 PM2/21/12
to web...@googlegroups.com
bulk insert fails?  or bulk insert does not return a list of IDs?

i assert that bulk insert does indeed work (at least for me 1.99.4), but that it does not return a list of IDs.

google provides no way to see the IDs it has assigned in a bulk insert unless you have specified the IDs yourself (which i don't think that the web2py DAL supports at this time).  i could be wrong - if you see in the google docs that they do return a list of IDs i'm happy to help with a patch!

cfh

BrendanC

unread,
Feb 21, 2012, 10:38:07 PM2/21/12
to web...@googlegroups.com
howesc,

My bad - you are correct. I retested bulk insert on GAE and it does work. I made a mistake with my syntax:

Instead of:

    imgids = db.imagetag.bulk_insert(ddlist)

I was using:

   imgids = db.imagetag.bulk_insert(**ddlist)

Complete example:

    #Test example - Build a list of dicts to insert
    ddlist = []
    d1 = {'image_id':10, 'tag_id':101 }
    d2 = {'image_id':10, 'tag_id':102 }
    d3 = {'image_id':11, 'tag_id':333 }
    ddlist.append(d1)
    ddlist.append(d2)
    ddlist.append(d3)
    imgids = db.imagetag.bulk_insert(ddlist)

   Unfortunately as you stated earlier, this inserted id's are not returned - just a True/False indicator.

Christian Foster Howes

unread,
Feb 22, 2012, 2:36:12 AM2/22/12
to web...@googlegroups.com, BrendanC
yes, bulk_insert changed from needing a dict to needing the ** notation
at some point. are the docs out of sync?

and look at this:
http://code.google.com/appengine/docs/python/datastore/functions.html#put
looks like GAE bulk PUT does return a list of keys. perhaps i smell a
DAL enhancement coming on!

cfh

Massimo Di Pierro

unread,
Feb 22, 2012, 9:18:24 AM2/22/12
to web2py-users
Please open a ticket. I will take a patch about this.

On Feb 22, 1:36 am, Christian Foster Howes <how...@umich.edu> wrote:
> yes, bulk_insert changed from needing a dict to needing the ** notation
> at some point.  are the docs out of sync?
>
> and look at this:http://code.google.com/appengine/docs/python/datastore/functions.html...
Reply all
Reply to author
Forward
0 new messages