id.max()

381 views
Skip to first unread message

Annet

unread,
Jun 18, 2012, 2:20:23 AM6/18/12
to web...@googlegroups.com
In a function I need the id of the last record created in a previous session. I tried:

maxID=db(db.Node).select(db.Node.id.max()).first().id

... but that results in a key error on id.

In a view, this {{=maxID}}, is being rendered as:

<Row {'_extra': <Row {'MAX(Node.id)': 2035}>}>

How do I get maxID=2035


Kind regards,

Annet

Anthony

unread,
Jun 18, 2012, 2:39:01 AM6/18/12
to web...@googlegroups.com
id_max = db.Node.id.max()
maxID
= db(db.Node).select(id_max).first()[id_max]

The key used to store the max value is actually the SQL expression generated by the .max() method, so the following should work as well:

maxID = db(db.Node).select(db.Node.id.max()).first()['MAX(Node.id)']



Anthony

Vasile Ermicioi

unread,
Jun 18, 2012, 4:59:50 AM6/18/12
to web...@googlegroups.com
or

max_id = db(db.Node).select(db.Node.id, orderby=~db.Node.id, limitby=(0,1)).first().id

Annet

unread,
Jun 19, 2012, 6:38:33 AM6/19/12
to web...@googlegroups.com
Hi Anthony and Vasile,

Thanks for your replies and explanation, problem solved.

Kind regards,

Annet

Reply all
Reply to author
Forward
0 new messages