" $ python manage.py db migrate" make"选项值必须是字符串"在烧瓶中迁移

时间:2018-03-25 03:27:50

标签: python flask

我正在学习使用flask-migrate但遇到问题〜

我跑

python run.py db init

这没有问题。然后,

python manager.py db migrate

但是在“迁移”之后命令,终端显示:

Traceback (most recent call last):
  File "manager.py", line 14, in <module>
    manager.run()
  File "/anaconda3/lib/python3.6/site-packages/flask_script/__init__.py", line 417, in run
    result = self.handle(argv[0], argv[1:])
  File "/anaconda3/lib/python3.6/site-packages/flask_script/__init__.py", line 386, in handle
    res = handle(*args, **config)
  File "/anaconda3/lib/python3.6/site-packages/flask_script/commands.py", line 216, in __call__
    return self.run(*args, **kwargs)
  File "/anaconda3/lib/python3.6/site-packages/flask_migrate/__init__.py", line 197, in migrate
    version_path=version_path, rev_id=rev_id)
  File "/anaconda3/lib/python3.6/site-packages/alembic/command.py", line 176, in revision
    script_directory.run_env()
  File "/anaconda3/lib/python3.6/site-packages/alembic/script/base.py", line 427, in run_env
    util.load_python_file(self.dir, 'env.py')
  File "/anaconda3/lib/python3.6/site-packages/alembic/util/pyfiles.py", line 81, in load_python_file
    module = load_module_py(module_id, path)
  File "/anaconda3/lib/python3.6/site-packages/alembic/util/compat.py", line 83, in load_module_py
    spec.loader.exec_module(module)
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "migrations/env.py", line 22, in <module>
    current_app.config.get('SQLALCHEMY_DATABASE_URI'))
  File "/anaconda3/lib/python3.6/site-packages/alembic/config.py", line 218, in set_main_option
    self.set_section_option(self.config_ini_section, name, value)
  File "/anaconda3/lib/python3.6/site-packages/alembic/config.py", line 245, in set_section_option
    self.file_config.set(section, name, value)
  File "/anaconda3/lib/python3.6/configparser.py", line 1192, in set
    self._validate_value_types(option=option, value=value)
  File "/anaconda3/lib/python3.6/configparser.py", line 1177, in _validate_value_types
    raise TypeError("option values must be strings")
TypeError: <flask_script.commands.Command object at 0x10a70f358>: option values must be strings

我的代码如下:

【manager.py】

from flask_script import Manager
from flask_migrate import Migrate,MigrateCommand
from project_demo_zl import app
from exts import db
from models import User

manager = Manager(app)

migrate = Migrate(app, db)

manager.add_command('db', MigrateCommand)

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

【config.py】

import os

HOSTNAME = '127.0.0.1'
PORT = '3306'
DATABASE = 'project_demo'
USERNAME = 'root'
PASSWORD = '****'

SQLALCHEMY_DATABASE_URL = "mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8".format(USERNAME, PASSWORD, HOSTNAME, PORT,
                                                                               DATABASE)

SQLALCHEMY_TRACK_MODIFICATIONS = True

DEBUG = True

SECRET_KEY = os.urandom(24)

有人可以帮我解释一下这个bug吗? 它几乎让我疯了 - - 非常感谢!!!!!!

1 个答案:

答案 0 :(得分:2)

错误与SQLALCHEMY_DATABASE_URI(URI)有关,但您的配置设置为SQLALCHEMY_DATABASE_URL(URL)。可能,变量默认为None,这就是错误抱怨值不是字符串的原因。尝试将配置变量更新为SQLALCHEMY_DATABASE_URI