Flask-model
Flask-SQLAlchemy
pip install flask-sqlalchemy
pip install pymysql
1.配置模块中
app.config['SQLALCHEMY_DATABASE_URI'] = "mysql+pymysql://username:password@hostname/database"
2.表关系
一对一
一对多
表示一对多的关系时,在子表类中通过foreign key(key)引用父表类。然后,在父表类中通过relationship()方法来引用子表的类
class Parent(Base):
__tablename__ = 'parent'
id = Column(Integer, primary_key = True)
children = relationship('Child')
# 夫类表中通过 relationship()方法来引用子表的类集合。
class Child(Base):
__tablename__ = ''child"
id = Column(Integer, primary_key = True)
parent_id = Column(Integer, ForeignKey('parent.id'))
多对多