Ah, ok.

So basically, because the computed field is stored and updated during
writes, it will get re-computed at every input (even if its
dependencies are not changed) and expects its dependencies to be
present in the input (ie, won't read the fields in the DB for missing
values).

I guess I should have figured it out that it would be pretty hard for
the Framework to extract the dependencies from the lambda expression
(well, catching the exception does work, though you are right that it
will leave possible syntax errors uncatched... tough cookie there).

In that case, I guess I'll look at virtual fields (where the value is
computed during extraction) for my needs instead.

Thanks for the insight.

On Dec 22, 7:04 pm, mdipierro <[email protected]> wrote:
> The problem is that you have compute attribute set at table definition
> so web2py tries to use it at every insert and every update. Even if in
> your update you is has no knowledge of the previous Alias_ordinal
> because you only updating Number_of_posts.
>
> There are two solutions... set db.table.field.compute=... only in the
> actions that need it.
>
> I can add a try except so that web2py does not tries to compute fields
> when there is not enough information. The downside is that a typo will
> result in a silent failure to compute. Perhaps we should do this
> anyway.
>
> Massimo
>
> On Dec 22, 4:56 pm, Magnitus <[email protected]> wrote:
>
> > Hi,
>
> > btw, I upgraded from 1.87 to 1.91 to see if the problem would get
> > resolved and 1.91 is loading my pages significantly faster. Also, the
> > revised version of the pdf manual is greatly superior to the original.
> > Thank you for that.
>
> > So, I tried to get fancy and use a computer field in my model and now
> > I'm getting an error (serves me right for trying to be fancy).
>
> > Either I'm doing something wrong or there is a bug. If I'm doing
> > something wrong, I'd like to know.
>
> > the relevant entry in model is as follow:
>
> > Forums_db.define_table('Forum_poster',
> >     Field('Alias', 'string', length=128, required=True),
> >     Field('Alias_ordinal', 'integer', default=1),
> >     Field('Alias_apparent', compute=lambda r: (r['Alias'] if
> > r['Alias_ordinal']==1 else r['Alias']+'['+str(r['Alias_ordinal'])
> > +']')),
> >     Field('Email', length=128, required=True),
> >     Field('Number_of_posts', 'integer', default=0),
> >     Field('Banned', 'boolean', default=False))
>
> > Whenever I try to write in the DB (insert or update), I get a key
> > error on 'Alias_ordinal'.
>
> > The relevant parts of the error log are as follow:
>
> > Last entry pertaining to my code:
>
> >   File "applications\Ascent\modules\Forum_tools.py", line 140, in
> > Submit_thread
>
> > DB(DB.Forum_poster.id==Poster['ID']).update(Number_of_posts=Poster['Number_of_posts']
> > +1)
>
> > Last entry pertaining to gluon internals:
>
> >   File "G:/Game_0_42/Code/web2py/applications/Ascent/models/
> > Forums.py", line 21, in <lambda>
> >     Field('Alias_apparent', compute=lambda r: (r['Alias'] if
> > r['Alias_ordinal']==1 else r['Alias']+'['+str(r['Alias_ordinal'])
> > +']')),
> >   File "G:\Game_42\Code\web2py\gluon\dal.py", line 3054, in
> > __getitem__
> >     return dict.__getitem__(self, key)
> > KeyError: 'Alias_ordinal'
>
> > Some additional debug info from the log:
>
> > Function argument list
>
> > (self=<Row {'Number_of_posts': 1}>, key='Alias_ordinal')
> > Code listing
>
> >     def __getitem__(self, key):
> >         key=str(key)
> >         if key in self.get('_extra',{}):
> >             return self._extra[key]
> > return dict.__getitem__(self, key)
>
> >     def __call__(self,key):
> >         key=str(key)
> >         if key in self.get('_extra',{}):
>
> > Variables
> > self    <Row {'Number_of_posts': 1}>
> > dict.__getitem__        <method '__getitem__' of 'dict' objects>
> > builtindict     <type 'dict'>
> > key     'Alias_ordinal'
>
> > Any help in diagnosing the problem would be greatly appreciated.
> > Thanks.
>
>

Reply via email to