integer field can be None even with is_empty_or()

41 views
Skip to first unread message

Richard

unread,
Oct 12, 2017, 10:56:51 PM10/12/17
to web2py-developers
Hello,

I am trying to use validate_and_insert(), but fail to use it... I am not sure if it a kind of bug, but let me explain the use case so you can reproduce the error and confirm or reject this possibility...

I don't want to blame any one, but my code fail on this line introduce 2 years ago :

https://github.com/web2py/web2py/blame/master/gluon/validators.py#L636

Full code here : https://github.com/web2py/web2py/blob/fb4c114d85494ad6d79d69539adaae103d76e5ce/gluon/validators.py#L636

USE CASE

Need a table with an integer field not mandatory :

db.define_table('dummy_table',
                             Field('field1', 'string'),
                             Field('field2', 'integer', requires=IS_EMPTY_OR(IS_INT_IN_RANGE(0, 100)))

So, I expect not having and error nor an  AttributeError: 'NoneType' object has no attribute 'isdigit' (which is even more problematic it prevent me to do a legitimate operation) if I do :

db.dummy_table.validate_and_insert(field1='some_string')

I have web2py 2.14.6, but the piece of code triggering the error seems unchange, so I expect the issue I face to be present in the last web2py version too, but I didn't tested it yet...

I had look on pyDAL side but can see a lead there...

What I do understand of the situation is that web2py enforce that any id or interger field (I guess I would be ok with the former but the later doesn't make sens to me) be actually an int in any possible way and prevent an integer field to be empty or None, which you may would like to be empty depending of your database design...

Do I am right?? And should this piece of code be refactored in order that an integer field could be None?? The issue seems to be related to the fact to the usage of isdigit that is a string method and fail on a None...

I am not sure what would be the best way to fix that in case it is actually a bug without introduce any security issue or bug for other use case that would had required this check to be wrote that way.

Thanks

Richard

Anthony

unread,
Oct 15, 2017, 9:28:17 AM10/15/17
to web2py-developers
First, the web2py code you point to is part of the IS_IN_DB validator, so it would not be relevant to the sample app code you have shown. Second, I cannot reproduce the error in question using either the code you have shown below or a variation with IS_IN_DB in place of IS_INT_IN_RANGE (using web2py 2.15).

Anthony

Richard Vézina

unread,
Oct 16, 2017, 2:44:08 PM10/16/17
to web2py-d...@googlegroups.com
Hello Anthony,

I will more carefully investigate, so far is what I have been able to pull that could be the source of my issue, there is maybe other thing involve. I didn't try the dummy table above, I thought it would worked, as it seems to be the root cause of my issue... But let me have another look, I will also try to port my app over 2.15.4 see if it gone or not... I will need to upgrade at some point anyway...

Thanks

Richard

--
-- mail from:GoogleGroups "web2py-developers" mailing list
make speech: web2py-developers@googlegroups.com
unsubscribe: web2py-developers+unsubscribe@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-developers+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Richard Vézina

unread,
Oct 16, 2017, 3:45:56 PM10/16/17
to web2py-d...@googlegroups.com
:(

Sorry for the false positive... 

I figure it out... It happen with IS_IN_DB(), I was aware of that, so the above code wasn't sufficient...

The correct code would had been :

db.define_table('dummy_table', 
                             Field('field1', 'string'),
                             Field('field2', 'integer', requires=IS_IN_DB(some other table...)))

When I use validate_and_insert() in 2.14.6 it fail on .isdigit() and nonetype... But I had expected, to work around it puting IS_EMPTY_OR() around IS_IN_DB was fixing it. So, I guess I forgot to restart web2py shell, so model didn't get update after changing it, so I thougth it was buggy...

The issue doesn't even be present with requires=IS_IN_DB() only (no IS_EMPTY_OR()) in 2.15.4

My bad.

Thanks for help

Richard

Reply all
Reply to author
Forward
0 new messages