Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to do a join in sqlalchemy session query?

I need to find the equivalent of this query in sqlalchemy.

SELECT u.user_id, u.user_name, c.country FROM
table_user u , table_country c WHERE u.user_email = '[email protected]'

i tried this below code:

session.query(User).join(Country.country).filter(User.user_email == '[email protected]').first()

and this gave me below error :

  AttributeError: 'ColumnProperty' object has no attribute 'mapper'

can anyone give an example of join query with tables mapped to new class objects ?

like image 801
skoovill Avatar asked Jul 05 '11 09:07

skoovill


1 Answers

Try this, assuming your User mapper has a relationship to Country configured.

user, country = session.query(User, Country.country).join(Country).filter(User.user_email == '[email protected]').first()
like image 86
jd. Avatar answered Oct 02 '22 14:10

jd.