2014-01-26 56 views
4

我使用的SQLAlchemy在下面創建兩張表:sqlalchemy.exc.ProgrammingError:(ProgrammingError)沒有唯一約束匹配給定鍵引用的表

class Classroom(Base): 
    __tablename__ = 'classroom' 
    building = Column(String(256), primary_key = True) 
    room_no = Column(String(256), primary_key = True) 
    capacity = Column(Integer) 

class Section(Base): 
    __tablename__ = 'section' 
    course_id = Column(String(256), ForeignKey('course.course_id'), primary_key = True) 
    building = Column(String(256), ForeignKey('classroom.building')) 
    room_no = Column(String(256), ForeignKey('classroom.room_no')) 

但是,我得到的錯誤說:

sqlalchemy.exc.ProgrammingError: (ProgrammingError) there is no unique constraint matching given keys for referenced table "classroom" 

回答

11

我想通了,在這種情況下,我需要有一個多foreig主要制約因素: 改變了我的部分型號爲:

class Section(Base): 
    __tablename__ = 'section' 
    course_id = Column(String(256), ForeignKey('course.course_id'), primary_key = True) 
    building = Column(String(256)) 
    room_no = Column(String(256)) 
    __table_args__ = (ForeignKeyConstraint([building, room_no],[Classroom.building, Classroom.room_no]), {}) 

和一切正常。

+6

感謝您跟進,當你想出來,而不是放棄這個問題。 –

相關問題