sqlite-utils
sqlite-utils copied to clipboard
Reconsider not running convert functions against null values
I just got caught out by the fact that None
values are not processed by the .convert()
mechanism https://github.com/simonw/sqlite-utils/blob/0b7b80bd40fe86e4d66a04c9f607d94991c45c0b/sqlite_utils/db.py#L2504-L2510
I had run this code while working on #420 and I wasn't sure why it didn't work:
$ sqlite-utils add-column content.db articles score float
$ sqlite-utils convert content.db articles score '
import random
random.seed(10)
def convert(value):
global random
return random.random()
'
The reason it didn't work is that the newly added score
column was full of null
values.
I fixed it by doing this instead:
$ sqlite-utils add-column content.db articles score float --not-null-default 1.0
But this indicates to me that the design of convert()
here may be incorrect.
Fixing this would require a bump to 4.0 because it would break existing code.
The alternative would be to introduce a new ignore_nulls=True
parameter which users can change to ignore_nulls=False
. Or come up with better wording for that.