文章内容

2018/2/6 10:06:55,作 者: 黄兵

alembic.util.exc.CommandError: Directory migrations already exists

最近在使用Flask上面Flask-SQLAlchemy更新数据库的时候提示:

具体错误提示如下:

(venv) E:\Python\Material prices>python manage.py db init
E:\Python\Material prices\venv\lib\site-packages\flask_sqlalchemy\__init__.py:839: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True or False to suppress this warning.
  'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
Traceback (most recent call last):
  File "manage.py", line 28, in <module>
    manager.run()
  File "E:\Python\Material prices\venv\lib\site-packages\flask_script\__init__.py", line 417, in run
    result = self.handle(argv[0], argv[1:])
  File "E:\Python\Material prices\venv\lib\site-packages\flask_script\__init__.py", line 386, in handle
    res = handle(*args, **config)
  File "E:\Python\Material prices\venv\lib\site-packages\flask_script\commands.py", line 216, in __call__
    return self.run(*args, **kwargs)
  File "E:\Python\Material prices\venv\lib\site-packages\flask_migrate\__init__.py", line 106, in init
    command.init(config, directory, 'flask')
  File "E:\Python\Material prices\venv\lib\site-packages\alembic\command.py", line 42, in init
    raise util.CommandError("Directory %s already exists" % directory)
alembic.util.exc.CommandError: Directory migrations already exists

代码如下:

manage.py文件内容如下:

app = create_app(os.getenv('FLASK_CONFIG') or 'default')
manager = Manager(app)
migrate = Migrate(app, db)

@manager.command
def test():
    """Run the unit tests."""
    import unittest
    tests = unittest.TestLoader().discover('tests')
    unittest.TextTestRunner(verbosity=2).run(tests)


def make_shell_context():
    return dict(app=app, db=db, User=User, Role=Role)


manager.add_command("shell", Shell(make_context=make_shell_context))
manager.add_command("db", MigrateCommand)

if __name__ == '__main__':
    manager.run()

config.py文件内容如下:

basedir = os.path.abspath(os.path.dirname(__file__))


class Config:
    SECRET_KEY = os.environ.get(
        'SECRET_KEY') or 'mLZXlBhl7hoV39xt6PUsJI8N3UUPW4UO69e98lisAItyUTkI2TbplZTsDdfdM9ZG'
    SQLALCHEMY_COMMIT_ON_TEARDOWN = True
    MAIL_SERVER = os.environ.get('MAIL_SERVER', 'smtp.googlemail.com')
    MAIL_PORT = int(os.environ.get('MAIL_PORT', '587'))
    MAIL_USE_TLS = os.environ.get('MAIL_USE_TLS', 'true').lower() in \
                   ['true', 'on', '1']
    MAIL_USERNAME = os.environ.get('MAIL_USERNAME')
    MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD')
    FLASKY_MAIL_SUBJECT_PREFIX = '[Flasky]'
    FLASKY_MAIL_SENDER = 'Flasky Admin '
    FLASKY_ADMIN = os.environ.get('FLASKY_ADMIN')
    # Bootstrap flask config
    BOOTSTRAP_USE_MINIFIED = True
    BOOTSTRAP_SERVE_LOCAL = True
    BOOTSTRAP_CDN_FORCE_SSL = True

    @staticmethod
    def init_app(app):
        pass


class DevelopmentConfig(Config):
    DEBUG = True
    SQLALCHEMY_DATABASE_URI = os.environ.get(
        'DEV_DATABASE_URL') or 'mysql://Mateent'


class TestingConfig(Config):
    TESTING = True
    SQLALCHEMY_DATABASE_URI = os.environ.get(
        'TEST_DATABASE_URL') or 'mysql://Materialesting'


class ProductionConfig(Config):
    SQLALCHEMY_DATABASE_URI = os.environ.get(
        'TEST_DATABASE_URL') or 'mysql://MaterialDBA:pdf-lib/Materon'


config = {
    'developemnt': DevelopmentConfig,
    'testing': TestingConfig,
    'production': ProductionConfig,

    'default': DevelopmentConfig
}

解决方案:

不需要再次运行manage.py db init,因为迁移目录和迁移已经存在于构建的应用程序中,直接使用manage.py db upgrade更新数据即可。

参考资料:

“Directory migrations already exists” during init on Heroku

黄兵个人博客原创。

转载请注明出处:黄兵个人博客 - alembic.util.exc.CommandError: Directory migrations already exists

分享到:

发表评论

评论列表