8

I am using sqlalchemy to create two tables in the following:

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')) 

However, I got the error saying:

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

1 Answer 1

16

I just figured out, in this case I need to have a multicolumn foreig key constraints: Changed my section model to be:

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]), {}) 

and everything works.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for following up when you figured it out, rather than just abandoning the question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.