DAL: Table aliases

7 views
Skip to first unread message

Oleg

unread,
Jan 21, 2009, 3:28:19 PM1/21/09
to web2py Web Framework
Is there some better way to do the next task with DAL?

db.define_table('nodes',
SQLField('name'),
SQLField('parent_id', 'reference nodes'))

id1 = db.nodes.insert(name='root')
id2 = db.nodes.insert(name='child 1', parent_id=id1)
db.nodes.insert(name='child 2', parent_id=id2)


now I wanna get the name of parent node of 'child 2'

db(db(db.nodes.name=='child 2').select()[0].parent_id ==
db.nodes.id).select()[0].name


The problem is.. for that task it will be processed two sql query..
with table aliases it is possible to make it only in one query..
is it possible with current version of DAL?

mdipierro

unread,
Jan 21, 2009, 4:41:26 PM1/21/09
to web2py Web Framework
parent=db.nodes.as('parent')
db((db.nodes.name=='child 2')&(db.nodes.parent_id==parent.id)).select
(parent.name)[0].name

Oleg

unread,
Jan 21, 2009, 4:43:29 PM1/21/09
to web2py Web Framework
Awesome!! :) Thank you, Massimo
Reply all
Reply to author
Forward
0 new messages