FLASK - flask db init 시 Directory Migration 발생 에러 문의드립니다.

- flask db init시 발생 오류

(venv) XXX:trip rocky$ flask db init
-----h1------
-----h2------
**Error: Directory migrations already exists and is not empty**

터미널에서 해당 명령어 실행시 위와 같은 에러가 나고 있습니다. (폴더가 생성되었으니 정상적이라고 봐야할까요?)
단, DB upgrade를 하면,

(venv) XXX:trip rocky$ flask db upgrade
-----h1------
-----h2------
Traceback (most recent call last):
  File "/Users/rocky/Desktop/myproject/trip/venv/bin/flask", line 8, in <module>
    sys.exit(main())
  File "/Users/rocky/Desktop/myproject/trip/venv/lib/python3.8/site-packages/flask/cli.py", line 967, in main
    cli.main(args=sys.argv[1:], prog_name="python -m flask" if as_module else None)
  File "/Users/rocky/Desktop/myproject/trip/venv/lib/python3.8/site-packages/flask/cli.py", line 586, in main
    return super(FlaskGroup, self).main(*args, **kwargs)
  File "/Users/rocky/Desktop/myproject/trip/venv/lib/python3.8/site-packages/click/core.py", line 782, in main
    rv = self.invoke(ctx)
  File "/Users/rocky/Desktop/myproject/trip/venv/lib/python3.8/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/Users/rocky/Desktop/myproject/trip/venv/lib/python3.8/site-packages/click/core.py", line 1259, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/Users/rocky/Desktop/myproject/trip/venv/lib/python3.8/site-packages/click/core.py", line 1066, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/Users/rocky/Desktop/myproject/trip/venv/lib/python3.8/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/Users/rocky/Desktop/myproject/trip/venv/lib/python3.8/site-packages/click/decorators.py", line 21, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "/Users/rocky/Desktop/myproject/trip/venv/lib/python3.8/site-packages/flask/cli.py", line 426, in decorator
    return __ctx.invoke(f, *args, **kwargs)
  File "/Users/rocky/Desktop/myproject/trip/venv/lib/python3.8/site-packages/click/core.py", line 610, in invoke
    return callback(*args, **kwargs)
  File "/Users/rocky/Desktop/myproject/trip/venv/lib/python3.8/site-packages/flask_migrate/cli.py", line 134, in upgrade
    _upgrade(directory, revision, sql, tag, x_arg)
  File "/Users/rocky/Desktop/myproject/trip/venv/lib/python3.8/site-packages/flask_migrate/__init__.py", line 96, in wrapped
    f(*args, **kwargs)
  File "/Users/rocky/Desktop/myproject/trip/venv/lib/python3.8/site-packages/flask_migrate/__init__.py", line 271, in upgrade
    command.upgrade(config, revision, sql=sql, tag=tag)
  File "/Users/rocky/Desktop/myproject/trip/venv/lib/python3.8/site-packages/alembic/command.py", line 298, in upgrade
    script.run_env()
  File "/Users/rocky/Desktop/myproject/trip/venv/lib/python3.8/site-packages/alembic/script/base.py", line 489, in run_env
    util.load_python_file(self.dir, "env.py")
  File "/Users/rocky/Desktop/myproject/trip/venv/lib/python3.8/site-packages/alembic/util/pyfiles.py", line 98, in load_python_file
    module = load_module_py(module_id, path)
  File "/Users/rocky/Desktop/myproject/trip/venv/lib/python3.8/site-packages/alembic/util/compat.py", line 184, in load_module_py
    spec.loader.exec_module(module)
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "migrations/env.py", line 27, in <module>
    str(current_app.extensions['migrate'].db.engine.url).replace('%', '%%'))
  File "/Users/rocky/Desktop/myproject/trip/venv/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 943, in engine
    return self.get_engine()
  File "/Users/rocky/Desktop/myproject/trip/venv/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 962, in get_engine
    return connector.get_engine()
  File "/Users/rocky/Desktop/myproject/trip/venv/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 556, in get_engine
    self._engine = rv = self._sa.create_engine(sa_url, options)
  File "/Users/rocky/Desktop/myproject/trip/venv/lib/python3.8/site-packages/flask_sqlalchemy/__init__.py", line 972, in create_engine
    return sqlalchemy.create_engine(sa_url, **engine_opts)
  File "/Users/rocky/Desktop/myproject/trip/venv/lib/python3.8/site-packages/sqlalchemy/engine/__init__.py", line 500, in create_engine
    return strategy.create(*args, **kwargs)
  File "/Users/rocky/Desktop/myproject/trip/venv/lib/python3.8/site-packages/sqlalchemy/engine/strategies.py", line 87, in create
    dbapi = dialect_cls.dbapi(**dbapi_args)
  File "/Users/rocky/Desktop/myproject/trip/venv/lib/python3.8/site-packages/sqlalchemy/dialects/mysql/mysqldb.py", line 118, in dbapi
    return __import__("MySQLdb")
ModuleNotFoundError: No module named 'MySQLdb'

이런식의 메시지가 나옵니다.

sqlite:///를 사용하면

flask db upgrade
-----h1------
-----h2------
INFO  [alembic.runtime.migration] Context impl SQLiteImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.

이런 메시지가 나오네요.


[제 소스 참고용 init.py]

from flask import Flask
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy

import config

db = SQLAlchemy()
migrate = Migrate()

print("-----h1------")

def create_app():
    app = Flask(__name__)
    app.config.from_object(config)

    # ORM
    db.init_app(app)
    migrate.init_app(app, db)

    # 블루프린트
    from .views import main_views
    app.register_blueprint(main_views.bp)

    print("-----h2------")

    return app

[제 소스 참고용 config.py]

import os

BASE_DIR = os.path.dirname(__file__)

SQLALCHEMY_DATABASE_URI = 'mysql:///{}'.format(os.path.join(BASE_DIR, 'board.db'))
SQLALCHEMY_TRACK_MODIFICATIONS = False

추가) 혹시 이 경우는 정상적으로 설치가 된 경우일까요? migrations 폴더가 있긴 합니다.
하지만, 별도의 db파일은 생성이 되지 않은 것 같구요..

[해당 템프 스토리지 사이트가 이미지게시를 막아 링크로 첨부]
https://tmpstorage.com/download/file/n7eo2oj0ga

답변해주시면 감사하겠습니다.

real7lucky 2208

M 2020년 11월 6일 10:20 오후

목록으로
1개의 답변이 있습니다. 1 / 1 Page

데이터베이스를 바꾸시면 migrations 폴더를 삭제하신 후 flask db init을 해 주시면 됩니다.
왜냐하면 현재 migrations 폴더에는 sqlite에 맞게 파일들이 구성된 상태이기 때문입니다.

다음 URL도 참고해 보세요.

https://wikidocs.net/81083#_3

박응용

M 2020년 11월 6일 10:09 오후