SQLAlchemy: How to properly use Relationship()?
<p>I went through the docs, and <strong>think</strong> I've structured everything correctly, but struggling to implement it. </p>

<p>There's two pieces to this: The use case is when I look at a resume, each resume has multiple jobs. Then the sum of all of those jobs determines the value of the entire resume. </p>

<p>I've set up two tables &amp; corresponding classes. </p>

<pre><code>from sqlalchemy import Column, Integer, String
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm import relationship
from sqlalchemy import ForeignKey

Base = declarative_base()
engine = create_engine('sqlite:///candidate.db', echo=True)


class Candidate_Evaluation(Base):
    __tablename__ = 'candidate_evaluation'
    id = Column(Integer, primary_key=True)
    label = Column(String)
    url = Column(String)
    string_of_job_evals = Column(String)
    job_evaluation_relationship = relationship("Job_Evaluation", back_populates="candidate_evalution")
    def __repr__(self):
        "&lt;Candidate(URL = '%s', label = '%s', job evaluations = '%s')&gt;" % (self.url, self.label, self.string_of_job_evals)


class Job_Evaluation(Base):
    __tablename__ = 'job_evaluation'
    id = Column(Integer, primary_key=True)
    candidate_evaluation_id = Column(Integer, ForeignKey('candidate_evaluation.id')) #
    details = Column(String)
    label = Column(String)
    candidate_evaluation_relationship = relationship("Candidate_Evaluation", back_populates='job_evaluation')
    def __repr__(self):
        "&lt;Job(label = '%s', details = '%s')&gt;" %(self.label, self.details)


Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine)
session = Session()

job = Job_Evaluation(label = 'front_end', details = 'javascript jquery html css')
session.add(job)
session.commit()
</code></pre>

<p>However, I'm running into a problem when I try to add records to the <code>job_evaluation</code> table. I <strong>think</strong> it has to do with how I've set up the relationship between them.</p>

<p>The goal is that I can add job evaluation to the database and then link it to the candidate evaluation. It's a Many to One relationship, but the Many comes first. Is that possible? </p>

<p>I'm getting the error: </p>

<p><code>sqlalchemy.exc.InvalidRequestError: Mapper 'Mapper|Job_Evaluation|job_evaluation' has no property 'candidate_evalution'</code></p>

<p>What am I doing wrong?</p>

<p>I struggled with this in my early days of using sqlalchemy.</p>

<p>As stated in the docs:</p>

<blockquote>
  <p>Takes a string name and has the same meaning as backref, except the complementing property is not created automatically, and instead must be configured explicitly on the other mapper. The complementing property should also indicate back_populates to this relationship to ensure proper functioning.</p>
</blockquote>

<pre><code>class Candidate_Evaluation(Base):
    __tablename__ = 'candidate_evaluation'
    id = Column(Integer, primary_key=True)
    label = Column(String)
    url = Column(String)
    string_of_job_evals = Column(String)
    job_evaluation_relationship = relationship("Job_Evaluation", backref ="candidate_evalution")
    def __repr__(self):
        "&lt;Candidate(URL = '%s', label = '%s', job evaluations = '%s')&gt;" % (self.url, self.label, self.string_of_job_evals)


class Job_Evaluation(Base):
    __tablename__ = 'job_evaluation'
    id = Column(Integer, primary_key=True)
    candidate_evaluation_id = Column(Integer, ForeignKey('candidate_evaluation.id')) #
    details = Column(String)
    label = Column(String)
    candidate_evaluation_relationship = relationship("Candidate_Evaluation", backref = job_evaluation')
    def __repr__(self):
        "&lt;Job(label = '%s', details = '%s')&gt;" %(self.label, self.details)
</code></pre>

<p>Just replace <code>back_populates</code> with <code>backref</code>.</p>

python