Do this instead try: urlID = db.web_page(url=url).id except (KeyError,TyepError): urlID = db.web_page.insert(url=url)
or better row = db.web_page(url=url) urlID = row and row.id or db.web_page.insert(url=url) If db.web_page(url=url) fails with an OperationalError (database error) you should not catch it without rollback. Massimo On Jul 2, 5:05 pm, Nick Arnett <nick.arn...@gmail.com> wrote: > Getting more comfortable with Web2Py and there sure is a lot to like about > it. > > I'm wondering what is considered best practice for inserting records that > might already exist, when I also want to get the id of the record if it does > exist. I haven't come across a shortcut for this. > > For now, I'm using a try statement, as below, for a table that stores web > page data: > > try: > urlID = db.web_page(url=url).id > except: > urlID = db.web_page.insert(url=url) > > I notice that early last year there was a discussion of how to create the > equivalent of Django's get_or_create(), which does this. I don't see that > that even made its way into Web2Py. > > NIck