flask连接不同数据库同名表时报错:Table ‘xxx‘ is already defined for this MetaData instance.

使用flask-sqlalchemy连接两个不同的数据库,这两个数据库中存在表名相同的表。启动时报错:

sqlalchemy.exc.InvalidRequestError: Table 'xxxx' is already defined for this MetaData instance.

解决办法:

第二个表使用一个新的metadata

from sqlalchemy import MetaData

class AmazonAsinHijack(db.Model):
    __tablename__ = 'AmazonAsinHijack'

    Id = Column(String(50, 'Chinese_PRC_CI_AS'), primary_key=True)
    SellerName = Column(String(50, 'Chinese_PRC_CI_AS'))

class AmazonAsinHijack_new(db.Model):
    __bind_key__ = 'newdb'
    __tablename__ = 'AmazonAsinHijack'
    metadata = MetaData()

    Id = Column(String(50, 'Chinese_PRC_CI_AS'), primary_key=True)
    SellerName = Column(String(50, 'Chinese_PRC_CI_AS'))

 

你可能感兴趣的:(数据库,数据库,mysql)