1

I am trying to query from two tables.. my appointments and clients. the client number is my foriegn key in appointments that I can pull from the clients database on.

Right now, I am just returning guests to see what it is doing, I am getting this error: TypeError: repr returned non-string (type tuple)

 @app.route('/calendar')
def weeklycal():
    weekyrnum=request.args.get('weekyr')
    guests = db.session.query(Appointments,Clients).filter(Appointments.clientnumber == Clients.clientnumber).filter(Appointments.weekyr == weekyrnum).all()
return 

render_template(calbyWeek.html",guests=guests)

How can I query everything from appointments and clients with clientnumber being the column to join on (which is defined as the foreign key in Appointments model), and filter by the week?

1 Answer 1

6

Reference the individual columns from multiple tables in you query, plus make sure you join to additional tables.

guests=db.session.query(Appointments.time,Clients.name).join(Clients).filter(Appointment.clientnumber==Clients.clientnumber).filter(Appointments.weekyr==weekyrnum).all()

If all you actually want is the guests but use Appoitments in you filter then you also just need to add a join.

guests=db.session.query(Clients).join(Appointments).filter(Appointments.clientnumber==Clients.clientnumber).filter(Appointments.weekyr==weekyrnum).all()
Sign up to request clarification or add additional context in comments.

2 Comments

I want all columns in Appointments with just 2 columns from Clients.. What is the most efficient way to do this? do I need to define every single column in my query()?
columns = Appointments.__table__.columns+ [Clients.name, Client.other_column] db.session.query(*columns)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.