python - is it possible to avoid table name conflict when using two data model in Flask-sqlalchemy -
i have flask app using flask-sqlalchemy access mysql. want refactor data model in same app, created new version model file few of tables should have same name old table.
oldmodel.py class tablea(db.model): __tablename__ = 'tablea' ...
newmodel.py class tablea(db.model): __tablename__ = 'tablea' ...
write function, idea query data old metadata write new database new metadata after column data manipulation, report same table name conflict in flask-sqlalchemy. error message sqlalchemy.exc.invalidrequesterror: table 'tablea' defined metadata instance. specify 'extend_existing=true' redefine options , columns on existing table object.
i don't want use flask-migrate on old database, create new database. tried __bind_key__
, seems working in new table not working in exist table had data.
is possible avoid use 2 version of datamodel in same app? or other approach?
finally, found way that, changed model pure sqlalchemy style, like
oldmodel.py sqlalchemy.ext.declarative import declarative_base baseold = declarative_base() class tablea(baseold): __tablename__ = 'tablea'
newmodel.py sqlalchemy.ext.declarative import declarative_base basenew = declarative_base() class tablea(basenew): __tablename__ = 'tablea'
then create 2 engines different db corresponding session. easy query old db , write new db want. 1 trick when import same name class using 'from xxx import xxx xxx' avoid class name conflict.
another way if still use db.model of flask-sqlalchemy use bind_key feature. can split model class different database, 1 big limitation model class name should not conflict.
Comments
Post a Comment