在Flask-migrate ValueError中:位置15的连接字符串中的插值语法无效 [英] In Flask-migrate ValueError: invalid interpolation syntax in connection string at position 15

查看:130
本文介绍了在Flask-migrate ValueError中:位置15的连接字符串中的插值语法无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用flask migrate进行数据库创建&使用flask-sqlalchemy在烧瓶中进行迁移.

I am using flask migrate to for database creation & migration in flask with flask-sqlalchemy.

一切正常,直到我更改数据库用户密码包含"@",然后它停止工作,所以我根据以下内容更新了代码 在密码包含特殊字符时编写连接字符串

Everything was working fine until I changed my database user password contains '@' then it stopped working so, I updated my code based on Writing a connection string when password contains special characters

它适用于应用程序,但不适用于烧瓶迁移,它在迁移时显示错误

It working for application but not for flask-migration, Its showing error while migrating

即在python manage.py db migrate

ValueError: invalid interpolation syntax in u'mysql://user:p%40ssword@localhost/testdb' at position 15

此处的密码为p@ssword,并由urlquote进行了转义(请参见上面的问题链接).

Here password is p@ssword and its escaped by urlquote (see above question link).

完整错误堆栈:

Traceback (most recent call last):
  File "manage.py", line 20, in <module>
    manager.run()
  File "/usr/local/lib/python2.7/dist-packages/flask_script/__init__.py", line 412, in run
    result = self.handle(sys.argv[0], sys.argv[1:])
  File "/usr/local/lib/python2.7/dist-packages/flask_script/__init__.py", line 383, in handle
    res = handle(*args, **config)
  File "/usr/local/lib/python2.7/dist-packages/flask_script/commands.py", line 216, in __call__
    return self.run(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/flask_migrate/__init__.py", line 177, in migrate
    version_path=version_path, rev_id=rev_id)
  File "/usr/local/lib/python2.7/dist-packages/alembic/command.py", line 117, in revision
    script_directory.run_env()
  File "/usr/local/lib/python2.7/dist-packages/alembic/script/base.py", line 407, in run_env
    util.load_python_file(self.dir, 'env.py')
  File "/usr/local/lib/python2.7/dist-packages/alembic/util/pyfiles.py", line 93, in load_python_file
    module = load_module_py(module_id, path)
  File "/usr/local/lib/python2.7/dist-packages/alembic/util/compat.py", line 79, in load_module_py
    mod = imp.load_source(module_id, path, fp)
  File "migrations/env.py", line 22, in <module>
    current_app.config.get('SQLALCHEMY_DATABASE_URI'))
  File "/usr/local/lib/python2.7/dist-packages/alembic/config.py", line 218, in set_main_option
    self.set_section_option(self.config_ini_section, name, value)
  File "/usr/local/lib/python2.7/dist-packages/alembic/config.py", line 245, in set_section_option
    self.file_config.set(section, name, value)
  File "/usr/lib/python2.7/ConfigParser.py", line 752, in set
    "position %d" % (value, tmp_value.find('%')))
ValueError: invalid interpolation syntax in u'mysql://user:p%40ssword@localhost/testdb' at position 15

请帮助

推荐答案

migrations/env.py文件中,您将找到导致此问题的代码.

In the migrations/env.py file, you will find the code that is responsible for this issue.

config.set_main_option('sqlalchemy.url',
                       current_app.config.get('SQLALCHEMY_DATABASE_URI'))

如果SQLALCHEMY_DATABASE_URI中有%标志,则将导致错误.

If there are % signs in the SQLALCHEMY_DATABASE_URI, this will cause an error.

您可以通过编辑migrations/env.py文件并按如下所示更改有问题的行来解决此问题

You can solve this by editing the migrations/env.py file, and changing the offending line as follows

db_url_escaped = current_app.config.get('SQLALCHEMY_DATABASE_URI').replace('%', '%%')
config.set_main_option('sqlalchemy.url', db_url_escaped)

另请参见 set_main_option的文档:

请注意,此值将传递到ConfigParser.set,后者支持使用pyformat(例如%(some_value)s)进行变量插值.因此,必须转义不属于插值符号一部分的原始百分号,例如%%.给定值可以使用插值格式引用文件中已经存在的另一个值.

Note that this value is passed to ConfigParser.set, which supports variable interpolation using pyformat (e.g. %(some_value)s). A raw percent sign not part of an interpolation symbol must therefore be escaped, e.g. %%. The given value may refer to another value already in the file using the interpolation format.

这篇关于在Flask-migrate ValueError中:位置15的连接字符串中的插值语法无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆