python - SQLAlchemy: filtering multiple column values in a subquery -


in sql, can use in operator subquery so:

select * t1 (t1.some_int, t1.some_string) in (     select id, name t2 ) 

but unable translate sqlalchemy query. far know, in_ method works on 1 column. there way replicate functionality in sqlalchemy?

you use join instead of subquery. this:

select * t1 inner join t2 on t1.some_int = t2.id , t1.some_string = t2.name 

and in sqlalchemy:

t1:

class t1(declarativebase):     __tablename__ = 't1'      __table_args__ = {'mysql_engine': 'innodb'}      id = column(u'id', integer, primary_key=true)     some_int = column('some_int', integer)     some_str = column('some_str', string(45))      def __init__ (self, some_int, some_str):         self.some_int = some_int         self.some_str = some_str 

t2:

class t2(declarativebase):     __tablename__ = 't2'      __table_args__ = {'mysql_engine': 'innodb'}      id = column(u'id', integer, primary_key=true)     name = column('name', string(45))     data = column('data', string(45))      def __init__ (self, name, data):         self.name = name         self.data = data 

in source code:

data = session.query(t1).join(t2, and_(t1.some_int == t2.id, t1.some_string == t2.name)).all() 

in result engine generates sql:

select t1.some_int t1_some_int, t1.id t1_id t1 inner join t2 on t1.some_int = t2.id , t1.some_str = t2.name 

Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -