There are two tables

class Table1(Base): __tablename__ = "table1" id = Column(Integer, primary_key=True) text = Column(String, nullable=False) class Table2(Base): __tablename__ = "table2" id = Column(Integer, primary_key=True) text = Column(String, nullable=False) table1_id = Column(Integer, nullable=False, ForeignKey('table1.id')) 

How to write a query that returns all the records from Table1, provided that there is no one in the Table2 table, among the existing records, that does not refer to the Table1.id = Table2.table1_id link

Something similar to:

 if Table1.id not in Table2.table1_id.list: print(Table1.text) 

    1 answer 1

    Decision:

     db_session.query( Table1 ).filter( ~Table1.id.in_( query( Table2.table1_id ) ) )