SqlAlchemy joins into tables without foreign keys

I have two tables in SqlAlchemy

class T1(Record, SqlBase):
    __tablename__ = 'table1'
    __table_args__ = (PrimaryKeyConstraint('column'), {'autoload': True},)

class T2(Record, SqlBase):
    __tablename__ = 'table2'
    __table_args__ = (PrimaryKeyConstraint('column'), {'autoload': True},)

      

I want to join two tables in one common column

session.query(T1).join(session.query(T2), T1.column == T2.column)

      

But I am getting the error

InvalidRequestError: Could not find a FROM clause to join from.  Tried joining to 
... but got: Can't find any foreign key relationships 
between 'T1' and 'FromGrouping object'. Perhaps you
 meant to convert the right side to a subquery using alias()?

      

How do I fix this problem? There are no foreign keys in the table.

+3


source to share


1 answer


Helpful Doc

You can use join if both classes have a relationship, or you can write a non-join query like this



session.query(T1).filter(T1.column == T2.column)

      

+1


source







All Articles