Python自动化之sqlalchemy复合外键
复合外键用法
metadata = MetaData(engine)
classedu = Table('classedu', metadata,
# Column('qq', BigInteger, primary_key=True),
Column('classname', VARCHAR(20), primary_key=True),
Column('coursename', VARCHAR(20),primary_key=True))
##这是一个复合外键
student = Table('student', metadata,
Column('qq', BigInteger, primary_key=True),
Column('classname', VARCHAR(20)),
Column('coursename', VARCHAR(20)),
ForeignKeyConstraint(['classname', 'coursename'], ['classedu.classname', 'classedu.coursename'])
)
metadata.create_all(engine)
Base = declarative_base()
book_m2m_author = Table('book_m2m_author', Base.metadata,
Column('book_id',Integer,ForeignKey('books.id')),
Column('author_id',Integer,ForeignKey('authors.id')),
)
class Book(Base):
__tablename__ = 'books'
id = Column(Integer,primary_key=True)
name = Column(String(64))
pub_date = Column(DATE)
authors = relationship('Author',secondary=book_m2m_author,backref='books')
参考如下
http://docs.sqlalchemy.org/en/rel_1_1/core/constraints.html
感谢关注