我正在尝试按照本教程https://www.codementor.io/olawalealadeusi896/restful-api-with-python-flask-framework-and-postgres-db-part-1-kbrwbygx5制作应用 但是我一直坚持使用“ python manage.py db migration”运行迁移。我遇到了这个错误,无法识别它们的含义,也不知道如何解决这个问题,因为我是python的新手。
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增加了大量开销,并且'
Traceback (most recent call last):
File "manage.py", line 17, in <module>
manager.run()
File "/home/jebbyk/.local/share/virtualenvs/blog_rest-pkqspueH/lib/python3.6/site-packages/flask_script/__init__.py", line 417, in run
result = self.handle(argv[0], argv[1:])
File "/home/jebbyk/.local/share/virtualenvs/blog_rest-pkqspueH/lib/python3.6/site-packages/flask_script/__init__.py", line 386, in handle
res = handle(*args, **config)
File "/home/jebbyk/.local/share/virtualenvs/blog_rest-pkqspueH/lib/python3.6/site-packages/flask_script/commands.py", line 216, in __call__
return self.run(*args, **kwargs)
File "/home/jebbyk/.local/share/virtualenvs/blog_rest-pkqspueH/lib/python3.6/site-packages/flask_migrate/__init__.py", line 95, in wrapped
f(*args, **kwargs)
File "/home/jebbyk/.local/share/virtualenvs/blog_rest-pkqspueH/lib/python3.6/site-packages/flask_migrate/__init__.py", line 215, in migrate
version_path=version_path, rev_id=rev_id)
File "/home/jebbyk/.local/share/virtualenvs/blog_rest-pkqspueH/lib/python3.6/site-packages/alembic/command.py", line 214, in revision
script_directory.run_env()
File "/home/jebbyk/.local/share/virtualenvs/blog_rest-pkqspueH/lib/python3.6/site-packages/alembic/script/base.py", line 489, in run_env
util.load_python_file(self.dir, "env.py")
File "/home/jebbyk/.local/share/virtualenvs/blog_rest-pkqspueH/lib/python3.6/site-packages/alembic/util/pyfiles.py", line 98, in load_python_file
module = load_module_py(module_id, path)
File "/home/jebbyk/.local/share/virtualenvs/blog_rest-pkqspueH/lib/python3.6/site-packages/alembic/util/compat.py", line 173, 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 27, in <module>
'SQLALCHEMY_DATABASE_URI').replace('%', '%%'))
AttributeError: 'NoneType' object has no attribute 'replace'
这似乎是迁移> env.py
中此行的主要问题config.set_main_option('sqlalchemy.url', current_app.config.get('SQLALCHEMY_DATABASE_URI').replace('%', '%%'))
但是运行“ python manage.py db init”后会自动生成“ migrations”目录 而且我不确定它是否包含任何错误
答案 0 :(得分:0)
在your example之后。
SQLALCHEMY_DATABASE_URI
variable get from system environment
SQLALCHEMY_DATABASE_URI = os.getenv('DATABASE_URL')
所以。您需要从终端运行以下命令来设置系统环境变量
$ export DATABASE_URL= postgres://name:password@houst:port/blog_api_db
答案 1 :(得分:-1)
发生这种情况是因为您可能使用 sqlalchemy.engine.url.URL
类来创建数据库 url。它不会给你回字符串。而是 <class 'sqlalchemy.engine.url.URL'>
的对象。
解决方案
为避免错误,请在正在使用的迁移/env.py 中找到配置
current_app.config.get('SQLALCHEMY_DATABASE_URI')
并将其替换为:
str(current_app.config.get('SQLALCHEMY_DATABASE_URI'))
迁移将正常工作。