You should not access query.records since it may go away. You can do:

row = db.groups(name <http://db.groups.name/>='some_name') or 
db.groups.insert(...)

It is still two queries but a little faster because db.groups(name 
<http://db.groups.name/> = 'some_name') only looks for the first match.


On Tuesday, 3 June 2014 11:41:37 UTC-5, chuan137 wrote:
>
>
> Before I insert a record, I need to check whether the record is already in 
> database. If true, then I need to know the id of the record.
>
> Below is my implementation, I first does the query, and check whether 
> record exist or not
>
> query = db(db.groups.name == 'some_name').select()
> if query.records:
>     group_id = query[0].id
> else:
>     group_id = db.groups.insert(.....)
>
> I wonder is there any better way to do this task? may be use 
> update_or_insert ? does it return the id of updated or inserted record?
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to