my model definitions for both Employee and Leaves Applied table are given below two tables are :
# Class for creating employee table
class Employee(Base):
__tablename__ = "employee"
id = Column(Integer, primary_key=True, nullable=False)
name = Column(String(30), nullable=False)
designation = Column(Integer,
ForeignKey("designation.id",
use_alter=True,
name="fk_designation",
onupdate="CASCADE",
ondelete="RESTRICT"))
team = Column(Integer, ForeignKey("team.id",
use_alter=True,
name="fk_team",
onupdate="CASCADE",
ondelete="RESTRICT"))
workstation_id = Column(String(30), nullable=False)
phone_extension = Column(String(30), nullable=False)
leave_eligibility_id = Column(Integer,
ForeignKey("leaves_eligibility.id",
use_alter=True,
name="fk_leave_eligibility",
onupdate="CASCADE",
ondelete="RESTRICT"))
manager_id = Column(Integer, ForeignKey("employee.id",
use_alter=True,
name="fk_manager_id",
onupdate="CASCADE",
ondelete="RESTRICT"))
leave_approver_id = Column(Integer,
ForeignKey("employee.id",
use_alter=True,
name="fk_leaveapprover_id",
onupdate="CASCADE",
ondelete="RESTRICT"))
leave_recommender_id = Column(Integer,
ForeignKey("employee.id",
use_alter=True,
name="fk_leaverecommender_id",
onupdate="CASCADE",
ondelete="RESTRICT"))
def __init__(self, name, designation,team,workstation_id, phone_extension,leave_eligibility_id,manager_id,leave_approver_id,leave_recommender_id):
self.name = name
self.designation = designation
self.team = team
self.workstation_id = workstation_id
self.phone_extension = phone_extension
self.leave_eligibility_id = leave_eligibility_id
self.manager_id = manager_id
self.leave_approver_id = leave_approver_id
self.leave_recommender_id = leave_recommender_id
# Class for creating team table
class LeavesApplied(Base):
__tablename__ = "leaves_applied"
id = Column(Integer, primary_key=True, nullable=False)
employee_id = Column(Integer,
ForeignKey("employee.id",
use_alter=True,
name="fk_emp_id",
onupdate="CASCADE",
ondelete="RESTRICT"))
from_date = Column(Date, nullable=False)
to_date = Column(Date, nullable=False)
half_day = Column(TINYINT(1))
type_id = Column("type_id",
mysql.ENUM("CASUAL_LEAVE",
"SICK_LEAVE",
"PRIVILEGED_LEAVE",
"ON_DUTY",
"AUTHORISED_LOSS_OF_PAY"))
decided_by_id = Column(Integer,
ForeignKey("employee.id",
use_alter=True,
name="fk_decided_by_id",
onupdate="CASCADE",
ondelete="RESTRICT"))
reason = Column(TEXT)
status = Column("status",
mysql.ENUM("RECOMMENDED",
"DECLINED",
"APPROVED",
"PENDING"))
recommended_by_id = Column(Integer,
ForeignKey("employee.id",
use_alter=True,
name="fk_recommended_by_id",
onupdate="CASCADE",
ondelete="RESTRICT"))
children=relationship("Employee",
backref=backref('parent',remote_side=[employee_id]),
foreign_keys='LeavesApplied.employee_id',
# Children will be represented as a dictionary
# On the attribute of manager_id
collection_class=attribute_mapped_collection(
'id'),)
def __init__(self,employee_id,from_date,to_date,half_day,type_id,decided_by_id,reason,status,rec ommended_by_id):
self.employee_id=employee_id
self.from_date = from_date
self.to_date = to_date
self.half_day = half_day
self.type_id = type_id
self.decided_by_id=decided_by_id
self.reason = reason
self.status = status
self.recommended_by_id=recommended_by_id
am executing the below query
rows=self.db.query(LeavesApplied,Employee)
.join(Employee)
.filter(LeavesApplied.employee_id== Employee.id)
.filter(Employee.id==1255).all()
but its throwing an error
InvalidRequestError: Could not find a FROM clause to join from.
Tried joining to , but got: Can't determine join between'leaves_applied' and 'employee'; tables have more than one foreign key constraint relationship between them. Please specify the 'onclause' of this join explicitly.
The error message is absolutely correct. From the code that you posted there are three possibilities for joining the two tables:
And this is exactly what sqlalchemy is compaining about, as it does not know on which relationship
/oncluause
would you like to perform a join.
Replacing your code with the below should solve the problem. Here I assumed that you would like to join on the LeavesApplied.employee_id
, and therefore I commented out the filter
clause as it is redundand in this case:
rows = (session.query(LeavesApplied,Employee)
.join(Employee, LeavesApplied.children) #.join(Employee)
#.filter(LeavesApplied.employee_id== Employee.id)
.filter(Employee.id==1255).all()
)
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