I have the following model defined with Flask-SQLAlchemy:
"""models.py"""
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
skill_candidate = db.Table(
'SkillCandidate',
db.Column('skill_id', db.String, db.ForeignKey('skill.id')),
db.Column('candidate_id', db.Integer, db.ForeignKey('candidate.id')))
class Candidate(db.Model):
id = db.Column(db.Integer, primary_key=True)
skills = db.relationship("Skill", secondary=skill_candidate)
class Skill(db.Model):
id = db.Column(db.String, primary_key=True)
name = db.Column(db.String, nullable=False, unique=True)
What am trying to achieve is the following : I want to return all the candidates who possess skills provided in a list input (even ideally, a list of skill_id)
I tried the following :
def get_skilled_candidates(skill_ids):
return Candidate.query.join(skill_candidate).\
filter(and_(*[skill_candidate.c.skill_id == skill_id for skill_id in skill_ids])).\
all()
The aim was to filter all candidates for every skill and compose it with a and_ statement
It works well if I use a list of 1 item (it returns all candidates that possess the skill) but does not if I add more skills in the input list (even tho I have candidates in base that fit the criteria)
constraint_item_candidateandconstraint_item_candidate.cin your query?constraint_item_candidateis actually meant to beskill_candidate, the association table of Skill and Candidate. skill_candidate.c is the way of accessing column fields for adb.TableinstanceSQL select * from SkillCandidate where not (exists (select * from SkillCandidate where SkillCandidate.skill_id not in (1, 2)))But it results in returning an empty result (1 and 2 are the ids of the required skills)