2016-10-11 31 views
2

我瀏覽了文檔,並且認爲我已經正確地構建了所有內容,但努力實現它。SQLAlchemy:如何正確使用Relationship()?

這裏有兩件事:用例是當我看簡歷時,每個簡歷都有多個工作。那麼所有這些工作的總和就決定了整個履歷的價值。

我已經成立了兩個表&相應的類。

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): 
     "<Candidate(URL = '%s', label = '%s', job evaluations = '%s')>" % (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): 
     "<Job(label = '%s', details = '%s')>" %(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() 

不過,我遇到了一個問題,當我試圖記錄添加到job_evaluation表。我想認爲它與我如何建立他們之間的關係。

目標是,我可以將作業評估添加到數據庫,然後將其鏈接到候選人評估。這是一個多對一的關係,但許多先來。那可能嗎?

,我發現了錯誤:

sqlalchemy.exc.InvalidRequestError: Mapper 'Mapper|Job_Evaluation|job_evaluation' has no property 'candidate_evalution'

我在做什麼錯?

+0

類屬性是'candidate_evaluation_relationship',不'candidate_evaluation',因此任一更改爲'關係(...,back_populates = 「candidate_evalution_relationship」)'或重命名屬性。 –

+0

啊,woops。在沒有意識到的情況下做出改變。謝謝! –

回答

0

在我使用sqlalchemy的早期階段,我苦苦掙扎。

如文檔指出:

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.

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): 
     "<Candidate(URL = '%s', label = '%s', job evaluations = '%s')>" % (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): 
     "<Job(label = '%s', details = '%s')>" %(self.label, self.details) 

只是backref取代back_populates

+0

對於那些通過閱讀文檔從頭開始學習所有東西的人來說,那可能也是用中文寫的:( –