我有一个简单的Author-Books模型,找不到将firstName和lastName用作复合键并在关系中使用它的方法。有任何想法吗?
from sqlalchemy import create_engine, ForeignKey, Column, String, Integer from sqlalchemy.orm import relationship, sessionmaker from sqlalchemy.ext.declarative import declarative_base Base = declarative_base() engine = create_engine('mssql://user:pass@library') engine.echo = True session = sessionmaker(engine)() class Author(Base): __tablename__ = 'authors' firstName = Column(String(20), primary_key=True) lastName = Column(String(20), primary_key=True) books = relationship('Book', backref='author') class Book(Base): __tablename__ = 'books' title = Column(String(20), primary_key=True) author_firstName = Column(String(20), ForeignKey('authors.firstName')) author_lastName = Column(String(20), ForeignKey('authors.lastName'))
问题在于您已将每个相关列分别定义为外键,如果这不是您想要的,那么您当然需要一个复合外键。Sqlalchemy通过说(以一种不太清楚的方式)对此做出响应,即无法猜测要使用哪个外键(firstName或lastName)。
firstName
lastName
声明复合外键的解决方案在声明性上有点笨拙,但仍然很明显:
class Book(Base): __tablename__ = 'books' title = Column(String(20), primary_key=True) author_firstName = Column(String(20)) author_lastName = Column(String(20)) __table_args__ = (ForeignKeyConstraint([author_firstName, author_lastName], [Author.firstName, Author.lastName]), {})
这里重要的是ForeignKey定义从各个列中删除,并且将aForeignKeyConstraint添加到__table_args__类变量中。有了这个,relationship定义就Author.books可以了。
ForeignKey
ForeignKeyConstraint
__table_args__
relationship
Author.books