proposed change of behavior

78 views
Skip to first unread message

Massimo Di Pierro

unread,
Dec 7, 2018, 2:41:18 AM12/7/18
to web2py-developers
Our friend Paolo has contributed a private patch that changes an incorrect web2py/pydal behavior.

Currently pydal treats id==0 the same as id==None. That means for example that table[0] = record is an insert, not an update. People may have used this in APIs

def post_in_table():
     table[request.args(0)] = request.vars

# post_in_table/0?key=value does insert
# post_in_table/1?key=value does update of id==1

Paolo pointed out that id can be 0 in some database and therefore id==0 should be treated as a possible valid id.

I think he is right but this would be a backward incompatible change. What do people think about changing this behavior?

Massimo

Michele Comitini

unread,
Dec 7, 2018, 3:23:39 AM12/7/18
to web2py-developers
Hi Massimo,
IMHO we should follow Paolo's suggestion.
Can we handle backward compatibility case adding a configurable behaviour in the __eq__() (https://docs.python.org/3/reference/datamodel.html#object.__eq__) method?


--
-- 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.


--
Michele Comitini

tel: +39 335 66 71 336

Leonel Câmara

unread,
Dec 12, 2018, 8:05:10 AM12/12/18
to web2py-developers
I think that's a bug, Paolo is right. It does change behavior so we should add a note to the release including this fix, but we can't keep bugs for backwards compatibility reasons. 

Anthony

unread,
Dec 12, 2018, 8:14:19 AM12/12/18
to web2py-developers
With this change, should we instead allow table[None] = dict(...) to do an insert, or just eliminate that syntax for doing inserts?

Anthony

Massimo DiPierro

unread,
Dec 12, 2018, 9:02:00 AM12/12/18
to web2py-d...@googlegroups.com
So far that syntax is allowed. I would keep it

--

Anthony

unread,
Dec 12, 2018, 12:07:45 PM12/12/18
to web2py-developers
On Wednesday, December 12, 2018 at 9:02:00 AM UTC-5, Massimo Di Pierro wrote:
So far that syntax is allowed. I would keep it

table[None] = dict(...)

is allowed, but it does not do an insert. Rather, it assigns str(None) to table.__dict__ with the value being the dictionary assigned.

Also, the current code has:

        elif str(key).isdigit():
           
if key == 0:
               
self.insert(**self._filter_fields(value))
           
elif self._db(self._id == key)\
                   
.update(**self._filter_fields(value)) is None:
               
raise SyntaxError('No such record: %s' % key)

Note, however, that when there is no record with id == key, .update() does not return None but rather 0. That means the SyntaxError above will never be raised, and instead attempted updates to non-existent IDs will fail silently. That's a problem in general but a particular problem for the proposed change, as any applications currently using table[0] = dict(...) will now fail silently rather than generating an error. I think we want to make sure an error is raised -- silent failures will be harder to detect and track down.

Anthony

Massimo DiPierro

unread,
Dec 12, 2018, 12:53:02 PM12/12/18
to web2py-d...@googlegroups.com
I agree. Will fix it

--
Reply all
Reply to author
Forward
0 new messages