Hi,

Why are field default lambdas being executed always (as part of 
define_table)?.

* Example:

   def xdefault(v):
       print '===== VALUE > %s' % v
       return v

   db.define_table(
       'xtable',
       Field('xfield1', default=lambda: xdefault('v1')),
       Field('xfield2', default=lambda: xdefault('v2')),
       Field('xfield3', default=lambda: xdefault('v3')),
   )

* Output:

   ===== VALUE > v1
   ===== VALUE > v2
   ===== VALUE > v3

* Stack trace for one of them:

   File C:\web2py\gluon\dal.py in define_table at line 4156
      
self._adapter.create_table(t,migrate=migrate,fake_migrate=fake_migrate,polymodel=polymodel)
   File C:\web2py\gluon\dal.py in create_table at line 551
      not_null = self.NOT_NULL(field.default,field.type)
   File C:\web2py\gluon\dal.py in NOT_NULL at line 750
      return 'NOT NULL DEFAULT %s' % self.represent(default,field_type)
   File C:\web2py\gluon\dal.py in represent at line 1200
      obj = obj()
   File C:\web2py\applications\test\models\db.py in <lambda> at line 88
      ... Field('xfield', default=lambda: xdefault('===== VALUE =====')), 
...
   File C:\web2py\applications\test\models\db.py in xdefault at line 83

I thought it was safe to put lambdas as field default values with the 
certainty that they won't be executed unless / until absolutely required 
(when new instances of the table are created without such default values), 
i.e. deferred execution.

Is this the expected behavior?.

p.s. I'm using trunk.

Thanks,

   Carlos

Reply via email to