row.update_record(effectiveSize-=1)

43 views
Skip to first unread message

Annet

unread,
Mar 23, 2013, 4:25:02 AM3/23/13
to web...@googlegroups.com
In defined a table with a field effectiveSize, after a user deleted a record from the child table
effectiveSize should be decreased by 1, does this work?

def onsuccess_related_names(form):
    row=db(db.groups.id==session.groupID).select(db.groups.effectiveSize).first()
    if row:
        row.update_record(effectiveSize-=1)


Kind regards,

Annet

Niphlod

unread,
Mar 23, 2013, 8:26:07 AM3/23/13
to web...@googlegroups.com
row.update_record(effectiveSize=row.effectiveSize-1)
has better chances

DenesL

unread,
Mar 23, 2013, 9:45:47 AM3/23/13
to web...@googlegroups.com
Or you could count them on the fly:

  effectiveSize = db(db.groups.id==session.groupID).count()

Annet

unread,
Mar 23, 2013, 10:05:48 AM3/23/13
to web...@googlegroups.com
Thanks you both for your replies, I don't know which solution to choose :-(
At the moment groups don't have 100+ members but that might change
in the near future I suppose in that case effectiveSize=row.effectiveSize-1
is faster than effectiveSize = db(db.groups.id==session.
groupID).count() isn't it? However, the latter will always result in the correct
value.

Kind regards,

Annet

Niphlod

unread,
Mar 23, 2013, 4:04:37 PM3/23/13
to web...@googlegroups.com
it's kinda unclear what you want from the posted snippet, however your original snippet would fail because of an invalid syntax (you can't do row.update_record(something-=1), no matter what)

from my pov, you asked for something able to decrease a column value of a preselected row by 1, and that is accomplishable with
row.update_record(something=row.something-1)

the second snippet suggested
effectiveSize = db(db.groups.id==session.groupID).count()

just counts how many groups.id are there with session.groupID as value.

So, you can do
row=db(db.groups.id==session.groupID).select(db.groups.effectiveSize).first()
if row:
    row.update_record(row.effectiveSize-=1)

or
count=db(db.groups.id==session.groupID).count()
if count:
    row=db(db.groups.id==session.groupID).select(db.groups.effectiveSize).first()
    row.update_record(effectiveSize-=1)

to have the same result, but you're just adding an unneccessary query.
If you plan to have a lot of groups the speed wouldn't matter in this case, cause groups.id is a primary key and your query either returns 1 row or 0 row. Counting or selecting will end up doing the same thing in more or less the same time, cause the biggest "operation" on a large scale table with that query would be the "filtering" part, the one in common with both methods, db.groups.id == session.groupID

A shorter version of a working snippet can also be
db(db.groups.id==session.groupID).update(effectiveSize=db.groups.effectiveSize-1)

in which case, if a group row is found it will get decreased, if not, nothing will happen.

Annet

unread,
Mar 24, 2013, 4:07:53 AM3/24/13
to web...@googlegroups.com
Thanks for your reply and explanation, I'll use:

db(db.groups.id==session.groupID).update(effectiveSize=db.groups.effectiveSize-1)


Kind regards,

Annet
Reply all
Reply to author
Forward
0 new messages