Have you tried this:
db.define_table('dog',
Field('name','string'),Field('owner',db.owner,default=db(db.owner.isdefault=True).select().first()))
On Thursday, June 7, 2012 2:20:30 PM UTC-7, Vincent wrote:
>
> Hi,
>
> I'm looking for a way to define a default foreign key dynamically.
>
> For example if I have the following schema:
>
> db.define_table('owner',Field('name', 'string'),
> Field('isdefault','boolean',default=False))
> db.people.insert('name'='Bill')
> db.people.insert('name'='unknown',isdefault=True)
> db.define_table('dog',
> Field('name','string'),Field('owner',db.owner,default=????))
> db.dog.insert(name='Fido',owner=1)
> db.dog.insert(name='mutt') # hopefully points to "unknown" in owner table
>
> Is there a way I could dynamically check the "owner" table and return the
> id of the first row where isdefault==True whenever I define a new "dog"
> without an owner?
> In the example above the "mutt" row should have owner==2 ("unknown" in the
> owner table).
>
> Trying to put a function as the default argument does not work. I realize
> I could set the owner to always be the first row in the table but I would
> like something more robust and flexible.
>
> Thanks
> Vincent
>
>