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 the two tables on some common column
session.query(T1).join(session.query(T2), T1.column == T2.column)
But I'm getting an 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 either table
relationship() will normally create a join between two tables by examining the foreign key relationship between the two tables to determine which columns should be compared.
In Flask-SQLAlchemy, the backref parameter in relationship method allows you to declare a new property under a specified class as seen in the example in their docs: class Person(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50)) addresses = db.relationship('Address', backref='person ...
One of the most sought after helpers being the handling of a database connection across the app. However, ensuring your database connection session is available throughout your app can be accomplished with base SQLAlchemy and does not require Flask-SQLAlchemy.
Useful Doc
You can use join if both class having relationship or you can write query without join like this
session.query(T1).filter(T1.column == T2.column)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With