Request: Can we make computed fields update when any of their dependencies are updated?

211 views
Skip to first unread message

Chris

unread,
May 22, 2014, 6:16:26 PM5/22/14
to
I found this report in the bug tracker, and I've had a similar problem. Here's the code to recreate the scenario:

# 1. Create table
db
.define_table('tablita',
 
Field('aa','double'),
 
Field('bb','double'),
 
Field('cc','double', compute=lambda r: r['aa']+r['bb']), migrate=True
 
)

# 2. Add record with appadmin.py
aa
=1
bb
=2
# Result cc=3 that's correct

# 3. But in controller
def suma():
 suma
= db(db.tablita.id==1).select().first()
 suma
.update_record(aa=2)

 
print suma ## <Row {'aa': 2, 'bb': 2.0, 'cc': 3.0, 'id': 1L}>
 
## is wrong, that no update computed fields, cc is still 3 when it should be 4

 message
= 'Hello'
 
return dict(message=message)

# What is the expected output?
cc
=4

# What do you see instead?
cc
=3

Every time I update one of my DB tables with a computed field, I'm surprised when the computation fails. Since the failure is silent, I don't know the computed field is going to be out of sync. I finally realized that the silence was causing me to overlook an important class of errors, and added required=True to every computed field where I'd want to be warned. (Basically, any computed field that uses more than 1 value is prone to this.)

I'm not sure what the best way to handle this is (include all the fields in the update? make computed fields not have a default value for required? track if the computed field depends on more than one other field?) but I just thought I'd +1 this issue and say I'd love any fixes/upgrades!

Thanks.

Massimo Di Pierro

unread,
May 22, 2014, 6:16:26 PM5/22/14
to web2py-d...@googlegroups.com
No.

But you can do

suma.update_record(aa=2, bb=suma.bb)


If you do not pass sufficient parameters for the computed field to recompute web2py would have to perform an extra DB query to get that information. We do not want to perform hidden queries.

On May 22, 2014, at 5:14 PM, Chris <ZUZFRE...@spammotel.com> wrote:

I found this report in the bug tracker, and I've had a similar problem:

# 1. Create table
db.define_table('tablita',
 Field('aa','double'),
 Field('bb','double'),
 Field('cc','double', compute=lambda r: r['aa']+r['bb']), migrate=True
 )

# 2. Add record with appadmin.py
aa=1
bb=2
# Result cc=3 that's correct

# 3. But in controller
def suma():
 suma = db(db.tablita.id==1).select().first()
 suma.update_record(aa=2)

 print suma ## <Row {'aa': 2, 'bb': 2.0, 'cc': 3.0, 'id': 1L}>
 ## is wrong, that no update computed fields, cc is still 3 when it should be 4

 message = 'Hello'
 return dict(message=message)

# What is the expected output?
cc=4

# What do you see instead?
cc=3

Basically, every time I use one of my database tables, I'm surprised because the computation fails silently, so I don't know the computed field is going to be out of sync. I finally realized that the silence was causing me to overlook an important class of errors, and added required=True to every computed field.

I'm not sure what the best way to handle this is (include all the fields in the update? make computed fields not have a default value for required?) but I just thought I'd +1 this issue and say I'd love any fixes/upgrades!

Thanks.

--
-- mail from:GoogleGroups "web2py-developers" mailing list
make speech: web2py-d...@googlegroups.com
unsubscribe: web2py-develop...@googlegroups.com
details : http://groups.google.com/group/web2py-developers
the project: http://code.google.com/p/web2py/
official : http://www.web2py.com/
---
You received this message because you are subscribed to the Google Groups "web2py-developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py-develop...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Chris

unread,
May 22, 2014, 6:19:37 PM5/22/14
to web2py-d...@googlegroups.com
True. Hidden queries would also be suboptimal. I think if I had some warning that my computed fields were failing on me that'd be enough - I suspect there are other people who have run into this issue.

Any way the update_record can be used on SQLFORM?

Massimo Di Pierro

unread,
May 22, 2014, 6:20:57 PM5/22/14
to web2py-d...@googlegroups.com
On May 22, 2014, at 5:19 PM, Chris <ZUZFRE...@spammotel.com> wrote:

True. Hidden queries would also be suboptimal. I think if I had some warning that my computed fields were failing on me that'd be enough - I suspect there are other people who have run into this issue.

Any way the update_record can be used on SQLFORM?

what do you mean?

Chris

unread,
May 22, 2014, 6:26:53 PM5/22/14
to web2py-d...@googlegroups.com
Say I had something like this:

        form = SQLFORM(db.page, page_selected(), fields=['text'])
       
if form.accepts(request.vars, session):

If I needed a field besides 'text' to compute the computed field, but didn't want it to be editable (at least, not in this form), is there a way to include it without tearing out SQLFORM completely?
To unsubscribe from this group and stop receiving emails from it, send an email toweb2py-developers+unsub...@googlegroups.com.

Massimo Di Pierro

unread,
May 22, 2014, 6:28:56 PM5/22/14
to web2py-d...@googlegroups.com
You should be able to inject the variables you need:


 form = SQLFORM(db.page, page_selected(), fields=['text’])
 form.vars.myothervar = ‘somevalue'
 if form.accepts(request.vars, session):

and computed fields are computed if the information is there.

To unsubscribe from this group and stop receiving emails from it, send an email toweb2py-develop...@googlegroups.com.

Chris

unread,
May 22, 2014, 7:17:59 PM5/22/14
to
Was this added before or after 2.7.5? I'm using the exact code below on one string field and one int field (tied to an id in another table), and gluon/html.py/FORM.accepts() is giving a 'value not in database' error for the int.

Update: I found this worked:

request.vars.myothervar = form.vars.myothervar = somevalue'
To unsubscribe from this group and stop receiving emails from it, send an emailtoweb2py-developers+unsubscr...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

-- 
-- mail from:GoogleGroups "web2py-developers" mailing list
make speech: web2py-d...@googlegroups.com
unsubscribe: web2py-develop...@googlegroups.com
details : http://groups.google.com/group/web2py-developers
the project: http://code.google.com/p/web2py/
official : http://www.web2py.com/
--- 
You received this message because you are subscribed to the Google Groups "web2py-developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email toweb2py-developers+unsub...@googlegroups.com.

Massimo DiPierro

unread,
May 22, 2014, 11:24:04 PM5/22/14
to web2py-d...@googlegroups.com
Always been there. 

On May 22, 2014, at 5:45 PM, Chris wrote:

Was this added before or after 2.7.5? I'm using the exact code below on one string field and one int field (tied to an id in another table), and gluon/html.py/FORM.accepts() is giving a 'value not found in database' error for the int.
To unsubscribe from this group and stop receiving emails from it, send an emailtoweb2py-developers+unsubscr...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

-- 
-- mail from:GoogleGroups "web2py-developers" mailing list
make speech: web2py-d...@googlegroups.com
unsubscribe: web2py-develop...@googlegroups.com
details : http://groups.google.com/group/web2py-developers
the project: http://code.google.com/p/web2py/
official : http://www.web2py.com/
--- 
You received this message because you are subscribed to the Google Groups "web2py-developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email toweb2py-developers+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
-- mail from:GoogleGroups "web2py-developers" mailing list
make speech: web2py-d...@googlegroups.com
unsubscribe: web2py-develop...@googlegroups.com
details : http://groups.google.com/group/web2py-developers
the project: http://code.google.com/p/web2py/
official : http://www.web2py.com/
---
You received this message because you are subscribed to the Google Groups "web2py-developers" group.

Chris

unread,
May 26, 2014, 12:17:16 PM5/26/14
to web2py-d...@googlegroups.com
Thanks Massimo! The request.vars.* = form.vars.* = x seemed to work, so I'm going to leave it as that for now.
Reply all
Reply to author
Forward
0 new messages