crud.create() get the id of last create record

155 views
Skip to first unread message

NewBeen

unread,
May 22, 2009, 3:18:37 PM5/22/09
to web2py Web Framework
I'm using the crud.create() function but i can not get the id of the
last create record,

I try to do like this in the controller

### CODE ###

response.flash='last id is %s' % form.vars.id
#####

Always return None

Wes James

unread,
May 22, 2009, 3:21:01 PM5/22/09
to web...@googlegroups.com
got to http://groups.google.com/group/web2py

then do a search on "id of last record" ;)

-wes

NewBeen

unread,
May 22, 2009, 3:28:41 PM5/22/09
to web2py Web Framework
The problem is that the answer is given just don´t work,
if you look to the code i post is the code in the answer and always
return none ;)


On May 22, 7:21 pm, Wes James <compte...@gmail.com> wrote:
> got tohttp://groups.google.com/group/web2py
>
> then do a search on "id of last record" ;)
>
> -wes
>

mdipierro

unread,
May 22, 2009, 3:34:23 PM5/22/09
to web2py Web Framework
if you use

form=SQFORM(db.table)
if form.accepts(request.vars,session):
# the id in form.vars.id
redirect(....)
return dict(form)

if you use

form=crud.create(db.table)

the accept and redirect are done inside crud create therefore there is
no scope where you can access the form.vars.id. You can do

form=crud.create(db.table,onaccept=lambda form: do_something_with
(for.vars.id))

that will be executed before redirection.

Wes James

unread,
May 22, 2009, 3:38:59 PM5/22/09
to web...@googlegroups.com
I also noticed in one of the posts that you can do:

db.executesql('select max(id) from mytable')

with sqlite.  sql may be db dependent.

-wes

mdipierro

unread,
May 22, 2009, 3:55:22 PM5/22/09
to web2py Web Framework
do not do this because if another thread has created a record
meanwhile you get the wrong result.

On May 22, 2:38 pm, Wes James <compte...@gmail.com> wrote:
> I also noticed in one of the posts that you can do:
>
> db.executesql('select max(id) from mytable')
>
> with sqlite.  sql may be db dependent.
>
> -wes
>

Wes James

unread,
May 22, 2009, 3:58:15 PM5/22/09
to web...@googlegroups.com
Wouldn't that always be the case for a multi-user system?

mdipierro

unread,
May 22, 2009, 4:06:44 PM5/22/09
to web2py Web Framework
not if you use form.vars.id, i.e. the value returned by db
[table].insert(...)



On May 22, 2:58 pm, Wes James <compte...@gmail.com> wrote:
> Wouldn't that always be the case for a multi-user system?
>
Reply all
Reply to author
Forward
0 new messages