Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

computed field problem on update record - Web2py 2.16.1 #1812

Open
PhanterJR opened this issue Nov 19, 2017 · 14 comments
Open

computed field problem on update record - Web2py 2.16.1 #1812

PhanterJR opened this issue Nov 19, 2017 · 14 comments

Comments

@PhanterJR
Copy link

I have this code:

my model:

def mysum(a,b):
    a=int(a)
    b=int(b)
    return a+b
db.define_table('mytable',
                Field('number1','integer'),
                Field('number2','integer'),
                Field('mysum', 'integer', compute=lambda row: mysum(row['number1'], row['number2']))
                )

my controller:

def index():
    if request.args(0):
        form=SQLFORM(db.mytable, request.args(0)).process()
    else:
        form=SQLFORM(db.mytable).process()
    if form.accepted:
        response.flash="Ok"
    else:
        print form.errors
        response.falsh=form.errors
    return dict(form=form)

my view:

{{extend 'layout.html'}}

{{block header}}
<center style="background-color: #333; color:white; padding:30px">
  <h1>/{{=request.application}}/{{=request.controller}}/{{=request.function}}
</center>
{{end}}


{{=form.custom.begin}}
    <div>Number1</div>
    {{=form.custom.widget.number1}}
    <br>
    <div>Number2</div>
    {{=form.custom.widget.number2}}
    <br>
    {{=form.custom.submit}}
{{=form.custom.end}}

on update (appadmin too), the record not update.

see in action (reproduce)
https://drive.google.com/file/d/0B7J2UqggMO7eaHNPTkEycUY3VlhNN2NCUkw3T1FwRWgyT1ZR/view?usp=sharing

https://drive.google.com/file/d/0B7J2UqggMO7eUDZ5NktpUTJubFlVVVd2dUhYZy14eEoyWXlB/view?usp=sharing

https://drive.google.com/file/d/0B7J2UqggMO7eNC1ZZEsyVUtFeFdHTGZNUFBTTXA0OFA3aFhF/view?usp=sharing

@ilvalle
Copy link
Contributor

ilvalle commented Nov 19, 2017

Does it work with R-2.15.4?

@PhanterJR
Copy link
Author

Yes, Work. I downgraded to this version.

@PhanterJR
Copy link
Author

There is no problem adding a new record, only when updating. Here I registered 1 + 2, in compute field it got 3, so I changed the value from 2 to 5 and made the submission. see here

does not show errors, apparently field number2 has been updated, but we can see that compute field has not changed. see here

When doing the refresh, nothing has changed. see here

@timnyborg
Copy link
Contributor

Same problem here. Computed fields now display, unless readable=False, and errors are thrown (but with no details), unless writable=False.

The solution for me is to set writable=False for all the computed fields, and they behave properly, as before.

@ilvalle
Copy link
Contributor

ilvalle commented Feb 4, 2018

@mdipierro what do you think?

@eyeteajay
Copy link

Hi all,
I am having the same issue. I believe it's caused by 2 problems in SQLFORM.accepts().
The first issue was sqlhtml.py:1736

        fields = {}
        for key in self.vars:
            fields[key] = self.vars[key]

        ret = FORM.accepts(...)

self.vars seems to be empty until after FORM.accepts populates it, so the for loop should go after accepts().

@eyeteajay
Copy link

The second issue sqlhtml.py:1892

            elif field.default is None and field.type != 'blob':
                self.errors[fieldname] = 'no data'
                self.accepted = False
                return False

This causes an early exit on computed fields and never gets to update the db.
I'm guessing the errors are never flashed on screen because there isn't any input control to connect to fieldname, since it's computed?

I don't know all of the logic behind this code, so I'm not quite sure how to fix this thoroughly. Hopefully one of the contributors can assign and patch this one now.

Thanks!

@tB0nE
Copy link

tB0nE commented Apr 16, 2018

Just to add my voice to the crowd, I am having the same problem, exact same symptoms as above, will probably revert version for now.

@leonelcamara
Copy link
Collaborator

This should be considered a DAL bug in my opinion, computed fields should be writable=False by default.

@eyeteajay
Copy link

Hi @leonelcamara ,
Perhaps writeable should be set by default by the DAL, but I think then the computed field still won't be updated. I've been using the attached file (extension changed for github) in my apps with limited testing and no problems so far.
sqlhtml.py.txt

@tB0nE
Copy link

tB0nE commented Sep 18, 2018

Problem appears to be gone in 2.17.1, can anyone else confirm?

@sbourgeois1
Copy link

Yes. This critical issue is now gone in version 17.2

@sbourgeois1
Copy link

But appears (again?) in case of update via dal (at least with postgresql), if you change the value of a field which is referred to by the compute function...
I don't know if this is new with the version 2.17.2 or not.

@leonelcamara
Copy link
Collaborator

@sbourgeois1 this happens if the update doesn't have all the fields the computed field needs, it's a very old issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants