No need for a loop just do fro the shell: db(db.persons.new_field==None).update(new_field=0)
Massimo On Tuesday, 26 July 2016 10:54:19 UTC-5, Ykä Marjanen wrote: > > How should I properly initialize old rows after inserting a new field if > default only updates the new rows? In my case the old rows should also work > with the new field and value. > > Should I do a for loop to manually update all 'old' rows to default value > after defining the new field? > > Ykä > > On Tuesday, July 26, 2016 at 5:05:54 PM UTC+3, Niphlod wrote: >> >> yep. "default" is something that gets evaluated in python at insert time. >> Given it takes also a callable, it can't be translated to a proper default >> constraint on the backend. When you alter the table, you should do so ALSO >> considering if it's full or empty, and/or that the new column would be >> meaningful only from a certain "release" onwards. >> >> tl;dr: the naming is unfortunate, but "default" doesn't translate to >> anything in the backend: all the machinery is done on the python layer. >> >> On Tuesday, July 26, 2016 at 10:13:19 AM UTC+2, Ykä Marjanen wrote: >>> >>> Hi, >>> >>> I have been using Web2py for couple years now, but have not been able to >>> find a decent solution to this issue. >>> >>> When creating a new web app I am constantly updating new fields to a >>> database table after I already have data there. When I add a new field, I >>> want it to have a default value for it, but for some reason web2py always >>> has the value at None for the new field even though I have defined it to be >>> 0. >>> >>> For example: >>> >>> db.define_table( >>> 'persons', >>> Field('old_field','integer',default=0), >>> Field('new_field','integer',default=0) >>> ) >>> >>> The new_field is now updated to the table correctly, but instead of 0 it >>> is None for the old records. >>> >>> This is annoying as I am assuming old tables to have the new_field also >>> set as 0 and now I must write code to assume None and change it manually to >>> 0. >>> >>> Am I missing something? >>> >>> Ykä >>> >>> -- Resources: - http://web2py.com - http://web2py.com/book (Documentation) - http://github.com/web2py/web2py (Source code) - https://code.google.com/p/web2py/issues/list (Report Issues) --- You received this message because you are subscribed to the Google Groups "web2py-users" group. To unsubscribe from this group and stop receiving emails from it, send an email to web2py+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/d/optout.