gpt4 book ai didi

python - "$ python manage.py db migrate"在 flask 迁移中生成 "option values must be strings"

转载 作者:行者123 更新时间:2023-12-01 02:02:19 30 4
gpt4 key购买 nike

我正在学习使用flask-migrate但是遇到一个问题~

我运行

python run.py db init

并且运行没有问题。那么,

python manager.py 数据库迁移

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

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)

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

最佳答案

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

关于python - "$ python manage.py db migrate"在 flask 迁移中生成 "option values must be strings",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49472259/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com