[英]fastAPI sqlalchemy - inner JOIN on 2 tables
I have a restapi up and running using the fastAPI framework, which is starting to work well.我使用 fastAPI 框架启动并运行了一个 restapi,它开始运行良好。
Now: I already have my MySQL code on how to inner join on 2 tables, and I want to be able to do the same, just using sqlalchemy.现在:我已经有了关于如何在 2 个表上进行内部连接的 MySQL 代码,我希望能够做到这一点,只需使用 sqlalchemy。
Firstly, here is my SQL code which works perfectly:首先,这是我的 SQL 代码,它运行良好:
select `the_user`.`email_adress`, `the_exercise`.`exercise_name`, `run_the_workout`.`repetitions`,`run_the_workout`.`sets`,`run_the_workout`.`pause_time`,`run_the_workout`.`day_to_perform_the_task`
from `workout_plan_task` `run_the_workout`
inner join `user_profiles` `the_user` on `run_the_workout`.the_user=`the_user`.user_id
inner join `exercises` `the_exercise` on `the_exercise`.`exercise_ID` = `run_the_workout`.`the_exercise`
WHERE `run_the_workout`.the_user=1
Now, here are my table models in SQL alchemy, representing the "user_profiles", "exercises" and the "workout_plan_task" table:现在,这是我在 SQL alchemy 中的表模型,分别代表“user_profiles”、“exercises”和“workout_plan_task”表:
#models.py: #models.py:
class UserProfiles(Base):
__tablename__ = "user_profiles"
user_ID = Column(Integer, primary_key=True, index=True)
email_adress = Column(String, unique=True)
age = Column(Integer)
sex = Column(Integer)
height = Column(Integer)
weight = Column(Integer)
main_goal = Column(Integer)
level_experience = Column(Integer)
profile_created_at = Column(Date)
class Exercises(Base):
__tablename__ = "exercises"
exercise_ID = Column(Integer, primary_key=True, index=True)
exercise_name = Column(String)
exercise_type = Column(String, nullable=True)
muscle_groups_worked_out = Column(String)
equipment_ID = Column(Integer, nullable=True)
class WorkOutPlanTask(Base):
__tablename__ = "workout_plan_task"
task_ID = Column(Integer, primary_key=True, index=True)
user_ID = Column(Integer, ForeignKey("user_profiles.user_ID"))
workout_plan_ID = Column(Integer, ForeignKey("workout_plan.workout_plan_ID"))
exercise_ID = Column(Integer, ForeignKey("exercises.exercise_ID"))
repetitions = Column(Integer)
sets = Column(Integer)
pause_time = Column(Integer)
day_to_perform_the_task = Column(String)
inside my "crud.py" file, im trying to run the query using inner joins:在我的“crud.py”文件中,我尝试使用内部连接运行查询:
#crud.py #crud.py
def get_workout_plan_for_user(db: Session, user_id:int):
return db.query(models.WorkOutPlanTask).join(models.UserProfiles, models.UserProfiles.user_ID == models.WorkOutPlanTask.user_ID).join(models.Exercises, models.Exercises.exercise_ID == models.WorkOutPlanTask.exercise_ID).filter(models.UserProfiles.user_ID == user_id)
and inside my #main.py i have this:在我的#main.py 里面我有这个:
@app.get("/all_workout_plan_tasks_for_user/{user_id}")
def get_workout_plan_for_user_by_userID(user_id: int, db:Session = Depends(get_db)):
db_workout_plan = crud.get_workout_plan_for_user(db, user_id=user_id)
if db_workout_plan is None:
raise HTTPException(status_code=404, detail="sorry.. no workoutplans found ..")
return [schemas.a_workout_plan_task.from_orm(v) for v in db.query(...)]
This gives me the error:这给了我错误:
sqlalchemy.exc.InvalidRequestError: SQL expression, column, or mapped entity expected - got 'Ellipsis'
Anyone here who could help me?这里有人可以帮助我吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.