How to build Relations on composite keys using sqlalchemy

54 Views Asked by At

Here are the sql requirements:

create table wt_question (
  question_id varchar(50),
  question_label text,
  primary key (question_id)
);


  create table question_answer (
  question_id varchar(50) references wt_question (question_id),
  answer_id varchar(50),
  primary key (question_id, answer_id)
);

create table result (
  question_id varchar(50) references wt_question (question_id),
  answer_id varchar(50),
  primary key (question_id, answer_id),
  foreign key (question_id, answer_id) references question_answer (question_id, answer_id)
);

I have issue to build that last ORM: my code:

class Result(Base):
    __tablename__ = "wt_result"
    __table_args__ = {"schema": "wallon"}
    question_id = sa.Column(sa.TEXT(length=50),sa.ForeignKey(WtQuestion.question_id), nullable=False, primary_key=True)
    answer_id = sa.Column(sa.TEXT(length=50), nullable=False,primary_key=True)
    __table_args__ = (
    ForeignKeyConstraint(["question_id", "answer_id"]),
)

error:

TypeError: ForeignKeyConstraint.__init__() missing 1 required positional argument: 'refcolumns'

Any friend can help ?

0

There are 0 best solutions below