http://web2py.com/books/default/chapter/29/6#belongs
belongs take a set or a _select() - mind the _ in front of the select - : you're using it in the wrong way :D db(db.movies.id.belongs( db(db.ratings.id>0)._select(db.ratings.imovie) )).select(db.movies.ALL) should work. If not, please post your model too... On Sunday, June 24, 2012 6:39:56 AM UTC+2, Vincenzo Ampolo wrote: > > Hi I'm trying do make a query: > > I want to get all the movies that have ratings on my database. > db.ratings is a many to many relationship with db.users and db.movies > > I did: > > In [7]: db(db.ratings.imovie.belongs(db.movies))._select(db.movies.ALL) > Out[7]: 'SELECT movies.id, movies.imdb_id FROM ratings, movies WHERE > (ratings.imovie IN (movies.id,movies.imdb_id));' > > Which is good but takes too much time. In particular i wanted to > restrict the IN clause to movies.id only so i did: > > In [8]: db(db.ratings.imovie.belongs(db.movies.id))._select(db.movies.ALL) > > Out[8]: 'SELECT movies.id, movies.imdb_id FROM ratings, movies WHERE > (1=0);' > > It replaced all the IN with a 1=0 clause. Very odd. > > Is it a bug or am I doing something wrong? > > > -- > Vincenzo Ampolo > http://vincenzo-ampolo.net > http://goshawknest.wordpress.com > > --