On Tue, Oct 6, 2009 at 23:36, Mengu <[email protected]> wrote:
>
> i got this error for the query:
>
>>>> items = db(db.posts.relations.category==db.categories.id).select()
> Traceback (most recent call last):
>  File "<console>", line 1, in <module>
>  File "/home/mengu/web2py/gluon/sql.py", line 1265, in __getattr__
>    return dict.__getitem__(self,key)
> KeyError: 'relations'

You don't have the key 'relations' in 'posts' table.

Try something like this:
query = (db.posts.id == db.relations.post) & (db.categories.id ==
db.relations.category)
db(query).select(db.posts.ALL, db.categories.ALL)

You should also use left join, but it depends what you want to select.

Note: we usually use singular names for tables.

> here is my table definitions:
> db.define_table('posts',
>    Field('title', 'string', length=200),
>    Field('slug', 'string', length=200),
>    Field('body', 'text'),
>    Field('html', 'text'),
>    Field('dateline', 'datetime', default=request.now,
> writable=False),
>    Field('user', db.auth_user)
> )
>
> db.define_table('comments',
>    Field('post', db.posts),
>    Field('author', 'string'),
>    Field('body', 'string', length=500),
>    Field('dateline', 'datetime', default=request.now, writable=False)
> )
>
> db.define_table('categories',
>    Field('title', 'string'),
>    Field('slug', 'string', length=200),
>    Field('postcount', 'integer')
> )
>
> db.define_table('relations',
>    Field('post', db.posts),
>    Field('category', db.categories)
> )
>
>
> On Oct 7, 4:43 am, mdipierro <[email protected]> wrote:
>> items = db(dd.posts.relations.category==db.categories.id).select()
>> for item in items:
>>     print item.posts.id,'belongs',item.categories.title
>>
>> On Oct 6, 8:23 pm, Mengu <[email protected]> wrote:
>>
>> > massimo,
>>
>> > actually this wasn't what i am asking, but thank you for showing
>> > another way for it. i have my controller named as "post" and i have
>> > the action "view". i currently have my routes_in like the 
>> > following:http://mengu.pastebin.com/m698f1726thisis working very well 
>> > however
>> > please let me know if this is not necessary or a proper way.
>>
>> > any recommendations on the query?http://mengu.pastebin.com/m111e1a47
>> > i could make something like:
>> > posts = db().select(db.posts.ALL)
>> > postcategories = {}
>> > for post in posts:
>> >     postcategories[post.id] = {}
>> >     for relation in post.relations.select():
>> >         category = db.categories[relation.category]
>> >         postcategories[post.id][category.id] = category.title
>>
>> > however this makes tons of queries for each post which is something i
>> > really don't want.
>>
>> > ----
>>
>> > dear alvaro,
>>
>> > i ofcourse would like to produce the software with you, i'd enjoy
>> > that. however i currently have my time very lack so i'm not working on
>> > the product all the time.
> >
>



-- 
 Álvaro Justen
 Peta5 - Telecomunicações e Software Livre
 21 3021-6001 / 9898-0141
 http://www.peta5.com.br/

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to