Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
Im trying to implement foreign key at one of my tables in flask-sqlalchemy, but keep getting this error all the time:
"specify a 'primaryjoin' expression." % self.prop
sqlalchemy.exc.NoForeignKeysError: Could not determine join condition between parent/child tables on rel ationship workout_plan_task.user_profiles - there are no foreign keys linking these tables. Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify a 'primaryjoin ' expression.
I have an "exercises" table, and I have an "user_profiles" tables. I have both of their primary keys are foreign keys in the "workout_plan_task" table.
Here is the code:
from flask_config import *
from datetime import datetime
from sqlalchemy.orm import relationship
class user_profiles(db.Model):
__tablename__ = "user_profiles"
user_ID = db.Column(db.Integer(), primary_key=True, autoincrement=True)
email_adress = db.Column(db.String())
age = db.Column(db.Integer())
sex = db.Column(db.Integer())
height = db.Column(db.Integer())
weight = db.Column(db.Integer())
main_goal = db.Column(db.Integer())
level_experience = db.Column(db.Integer())
profile_created_at = db.Column(db.DateTime(), default=datetime.utcnow())
def __init__(self, email_adress, age, sex, height, weight, main_goal, level_experience, profile_created_at):
self.email_adress = email_adress
self.age = age
self.sex = sex
self.height = height
self.weight = weight
self.main_goal = main_goal
self.level_experience = level_experience
self.profile_created_at = profile_created_at
def __repr__(self):
return '{}-{}-{}-{}'.format(self.email_adress, self.age, self.sex, self.height, self.weight, self.main_goal, self.level_experience, self.profile_created_at)
class workout_plan_task(db.Model):
__tablename__ = "workout_plan_task"
task_ID = db.Column(db.Integer(), primary_key=True)
user_ID = db.Column(db.Integer, db.ForeignKey('user_ID'), nullable=False, column_hide_backrefs = False)
workout_plan_ID = db.Column(db.Integer())
exercise_ID = db.Column(db.Integer, db.ForeignKey('exercise_ID'), nullable=False, column_hide_backrefs = False)
repetitions = db.Column(db.Integer())
sets = db.Column(db.Integer())
pause_time = db.Column(db.Integer())
day_to_perform_the_task = db.Column(db.String())
the_user = relationship("user_profiles", back_populates="workout_plan_task")
def __init__(self, task_ID, user_ID, workout_plan_ID, exercise_ID, repetitions, sets, pause_time, day_to_perform_the_task):
self.task_ID = task_ID
self.user_ID = user_ID
self.workout_plan_ID = workout_plan_ID
self.exercise_ID = exercise_ID
self.repetitions = repetitions
self.sets = sets
self.pause_time = pause_time
self.day_to_perform_the_task = day_to_perform_the_task
def __repr__(self):
return '{}-{}-{}-{}-{}-{}-{}-{}'.format(self.task_ID, self.user_ID, self.workout_plan_ID, self.exercise_ID, self.repetitions, self.sets, self.pause_time, self.day_to_perform_the_task)
class exercises(db.Model):
__tablename__ = "exercises"
exercise_ID = db.Column(db.Integer(), primary_key=True)
exercise_name = db.Column(db.String())
exercise_type = db.String(db.String())
muscle_groups_worked_out = db.String(db.String())
equipment_ID = db.String(db.Integer())
def __init__(self, exercise_ID, exercise_name, exercise_type, muscle_groups_worked_out, equipment_ID):
self.exercise_ID = exercise_ID
self.exercise_name = exercise_name
self.exercise_type = exercise_type
self.muscle_groups_worked_out = muscle_groups_worked_out
self.equipment_ID = equipment_ID
def __repr__(self):
return '{}-{}-{}-{}-{}'.format(self.exercise_ID, self.exercise_name, self.exercise_type, self.muscle_groups_worked_out, self.equipment_ID)
Could someone plz help me?
In the workout_plan_task
model, you should refer to the other tables as user_profiles.user_ID
rather than just user_ID
and exercises.exercise_ID
rather than just exercise_ID
so update those lines:
user_ID = db.Column(db.Integer, db.ForeignKey('user_profiles.user_ID'), nullable=False, column_hide_backrefs = False)
exercise_ID = db.Column(db.Integer, db.ForeignKey('exercises.exercise_ID'), nullable=False, column_hide_backrefs = False)
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.