小编今天在使用Sqlalchemy连接不同数据库,生成两个相同的表名的时候,出现了这样的错误Table 'XXX' is already defined for this MetaData instance. Specify 'extend_existing=True' to redefine options and columns on an existing Table object。导致第二个表无法生成,下面小编说下解决办法:

给第二个表使用一个新的metadata
代码示例:

from sqlalchemy import MetaData
 
class A(db.Model):
    __tablename__ = 'A'
 
    ···
 
class A(db.Model):
    __bind_key__ = 'newdb'
    __tablename__ = 'A'
    metadata = MetaData()
 
    ···
Last modification:December 17, 2020
If you think my article is useful to you, please feel free to appreciate