SQLAlchemy迁移不创建表

时间:2017-01-29 03:58:25

标签: python flask flask-sqlalchemy flask-migrate

我正在关注Miguel的this教程。有一个用于创建迁移的迁移脚本。 nullptr

db_migrate.py

为了升级数据库,它有import imp from migrate.versioning import api from app import db from config import SQLALCHEMY_DATABASE_URI from config import SQLALCHEMY_MIGRATE_REPO v = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) migration = SQLALCHEMY_MIGRATE_REPO + ('/versions/%03d_migration.py' % (v+1)) tmp_module = imp.new_module('old_model') old_model = api.create_model(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) exec(old_model, tmp_module.__dict__) script = api.make_update_script_for_model(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO, tmp_module.meta, db.metadata) open(migration, "wt").write(script) api.upgrade(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) v = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) print('New migration saved as ' + migration) print('Current database version: ' + str(v))

db_upgrade.py

现在每次运行from migrate.versioning import api from config import SQLALCHEMY_DATABASE_URI from config import SQLALCHEMY_MIGRATE_REPO api.upgrade(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) v = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO) print('Current database version: ' + str(v)) 时,它都会创建一个每次都包含相同代码的迁移文件:

db_migrate.py

如果我运行from sqlalchemy import * from migrate import * from migrate.changeset import schema pre_meta = MetaData() post_meta = MetaData() def upgrade(migrate_engine): # Upgrade operations go here. Don't create your own engine; bind # migrate_engine to your metadata pre_meta.bind = migrate_engine post_meta.bind = migrate_engine def downgrade(migrate_engine): # Operations to reverse the above upgrade go here. pre_meta.bind = migrate_engine post_meta.bind = migrate_engine ,它只会增加数据库中db_upgrade.py表中的迁移版本,但不会创建新表。 我是否需要手动编辑迁移文件以创建表?

配置:

migrate_version

这是我的SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://root:@localhost/microblog' SQLALCHEMY_MIGRATE_REPO = os.path.join(BASEDIR, 'db_repository')

models.py

0 个答案:

没有答案