You are welcome! :)

2014-05-28 11:08 GMT-03:00 'Michael Gheith' via web2py-users <
web2py@googlegroups.com>:

> Many thanks for this, I found it to be helpful :)
>
>
> On Wednesday, May 28, 2014 6:30:31 AM UTC-5, yamandu wrote:
>
>> Give a look here: http://web2py.com/books/default/chapter/29/06/the-
>> database-abstraction-layer#Legacy-databases-and-keyed-tables
>>
>> But I think it won't work in your case because name is a string.
>> Our you can model it another way.
>>
>> db.define_table('tiers',
>>     Field('name', 'string'),
>>     Field('created_on', 'datetime', default=request.now),
>>     format = '%(name)s')
>>
>> db.define_table('metrics',
>>     Field('tier_name', 'string', requires=IS_IN_DB(db, 'tiers.name
>> ',%(name)s')),
>>     Field('total_count', 'integer'),
>>     Field('total_size', 'bigint'),
>>     Field('created_on', 'datetime', default=request.now))
>>
>> However, this will not enforce reference at db level, only in application.
>> But you can query an join it by name without problems.
>>
>> query = db(db.tiers.name == db.metrics.tier_name).select()
>> metric = db(db.metrics.tier_name == 'name').select()
>>
>> or you can obviously write a helper function to short it
>>
>> get_metrics = lambda name: db(db.metrics.tier_name == name).select()
>> get_tier = lambda name: db(db.tier.name == name).select().first()
>>
>> tier = get_tier(name)
>> tier_metrics = get_metrics(name)
>>
>>
>> 2014-05-27 21:35 GMT-03:00 'Michael Gheith' via web2py-users <
>> web...@googlegroups.com>:
>>
>>> I have two pretty basic tables:
>>>
>>> db.define_table('tiers',
>>>     Field('name', 'string'),
>>>     Field('created_on', 'datetime', default=request.now),
>>>     format = '%(name)s')
>>>
>>> db.define_table('metrics',
>>>     Field('tier_id', 'reference tiers'),
>>>     Field('total_count', 'integer'),
>>>     Field('total_size', 'bigint'),
>>>     Field('created_on', 'datetime', default=request.now))
>>>
>>> I pre populated my tiers table with data.  When I store data in my
>>> metrics table, I don't know off hand the tier_id.  I know the tier name
>>> though.  Is there a way I can use the tier name instead of the tier_id in
>>> this reference?
>>>
>>> --
>>> 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+un...@googlegroups.com.
>>>
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>>
>>
>>
>> --
>> Att.
>>
>> Carlos J. Costa
>> Cientista da Computação
>> Esp. Gestão em Telecom
>>
>> EL MELECH NEEMAN!
>> אָמֵן
>>
>>   --
> 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.
>



-- 
Att.

Carlos J. Costa
Cientista da Computação
Esp. Gestão em Telecom

EL MELECH NEEMAN!
אָמֵן

-- 
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.

Reply via email to