Reputation: 5021
I'm trying to query multiple tables by joining and selecting specific columns using SQLAlchemy. My query looks like this this
data = db.session.query(
TableA,
TableB,
TableC,
).filter(
TableA.id == TableB.x_id,
TableA.id2 == TableB.y_id,
TableA.id3 == TableB.z_id,
).filter(
TableA.id == TableC.id_a,
TableA.id3 == TableC.id_b,
between(TableB.date, from_date, to_date)
).distinct(
TableA.id
).select(
TableA.id,
TableA.contact,
TableB.city,
TableC.source,
TableC.name,
).all()
When I run this I get an error
AttributeError: 'BaseQuery' object has no attribute 'select'
How can I select specific columns with this query or how can it be improved? The reason for selecting specific columns is that all of these tables have 10s of the columns and returning back all columns will be slow as it will have a lot of data. I tried to move the select
func to the beginning but it didn't help.
Upvotes: 2
Views: 1158
Reputation: 46
query object has no select, you should query then filter the result just like this
db.query(models.TableA, models.TableB, models.TableC).filter(modes.TableA.id)
and to grab the objects u should grab them by first()
or all()
Upvotes: 1