这是我的桌子:
CREATE TABLE "public"."patient_has_doctor" (
"patient_person_id" INT NOT NULL,
"doctor_person_id" INT NOT NULL,
PRIMARY KEY ("patient_person_id", "doctor_person_id"),
CONSTRAINT "fk_Patient_has_Doctor_Patient1"
FOREIGN KEY ("patient_person_id")
REFERENCES "public"."patients" ("person_id")
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT "fk_Patient_has_Doctor_Doctor1"
FOREIGN KEY ("doctor_person_id")
REFERENCES "public"."doctor" ("person_id")
ON DELETE CASCADE
ON UPDATE CASCADE);
我不确定如何在sqlalchemy中定义主键。我知道如何根据Stack上的其他帖子进行外键,但是这种特殊情况使我难以理解。我的猜测是:
class Book(Base):
__tablename__ = 'books'
author_firstName = Column(String(20))
author_lastName = Column(String(20))
__table_args__ = (ForeignKeyConstraint([author_firstName, author_lastName],
[Author.firstName, Author.lastName]),
{}, primary_key=True)
但是我不确定这是否是最好的方法。 来源:Relations on composite keys using sqlalchemy