Nik, declare your validators before you clone your model.

I've encountered this issue before and the problem was because I wasn't* 
*declaring 
validators of the "master" table *BEFORE* I define the new gholas (i.e. 
"clones") table. When I re-located the offending .requires immediately 
after the master table, inheritance worked as expected. 

Massimo, maybe it's worth a mention in the new book version, considering 
that its coding convention: "define models first,  validators later". It 
might save a few hours worth of head scratching and a lifetime of hair 
loss. :D

Here's a proposed entry: 

Table inheritance may not behave as expected if attributes or validators 
are left undefined before passing them to the *define_table*. If the cloned 
fields are expected to behave like the original, ensure attributes are 
declared prior to using them for defining subsequent models.

... 

####doesn't work: as expected 
>>>db.define_table('t_master', Field('f1'), Field('f2'))
>>>db.define_table('t_ghola', db.t_master, Field('owner', db.auth_user))
>>>db.t_master.f1.requires=IS_NOT_EMPTY() # defined before clone creation; 
validator not inherited
>>>db.t_master.f1.requires == db.t_ghola.f1.requires
*False*
#### inheritance works
>>>db.define_table('t_master', Field('f1'), Field('f2'))
>>>db.t_master.f1.requires=IS_NOT_EMPTY() #validator defined before clone 
table creation
>>>db.define_table('t_ghola', db.t_master, Field('owner', db.auth_user))
>>>db.t_master.f1.requires == db.t_ghola.f1.requires
*True*

*
*
P.S. Found a typo in the book,  Self-Reference and Aliases section, in case 
it hasn't been corrected yet. *Subtle*, misspelled as "subtile": "The 
difference is *subtile*, and there is ..."

Best,

Reply via email to