Flask生成表迁移时出现:sqlalchemy.exc.InvalidRequestError: Table ‘xxxr’ is already defined for this MetaData instance. Specify ‘extend_exis
ting=True’ to redefine options and columns on an existing Table object.
意思是说表XX已经定义。指定extend_exis用于重新定义现有表对象上的选项和列。
这里的原因是第一次通过定义python类声明SQLAlchemy Mapping时,类的定义会保存到元数据对象中,用以防止多个定义映射到同一个表所导致的冲突。所以我们可以加入:
class CinemaUser(BaseModel):
__table_args__ = {'extend_existing': True}
username=db.Column(db.String(32),unique=True)
再重新migrate,upgrade就ok了