我正在学习flask web微框架,在我的数据库初始化之后运行flask db init
我运行flask db migrate
,将我的模型类迁移到数据库,我收到了一个错误。我在Windows 10上工作,数据库是MySQL,扩展安装是flask-migrate
,flask-sqlalchemy
,flask-login
。
(env) λ flask db migrate
Traceback (most recent call last):
File "c:\python36\Lib\runpy.py", line 193, in _run_module_as_main
"__main__", mod_spec)
File "c:\python36\Lib\runpy.py", line 85, in _run_code
exec(code, run_globals)
File "C:\Users\aka\Dev\dream-team\env\Scripts\flask.exe\__main__.py", line 9, in <module>
File "c:\users\aka\dev\dream-team\env\lib\site-packages\flask\cli.py", line 513, in main
cli.main(args=args, prog_name=name)
File "c:\users\aka\dev\dream-team\env\lib\site-packages\flask\cli.py", line 380, in main
return AppGroup.main(self, *args, **kwargs)
File "c:\users\aka\dev\dream-team\env\lib\site-packages\click\core.py", line 697, in main
rv = self.invoke(ctx)
File "c:\users\aka\dev\dream-team\env\lib\site-packages\click\core.py", line 1066, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "c:\users\aka\dev\dream-team\env\lib\site-packages\click\core.py", line 1066, in invoke
return _process_result(sub_ctx.command.invoke(sub_ctx))
File "c:\users\aka\dev\dream-team\env\lib\site-packages\click\core.py", line 895, in invoke
return ctx.invoke(self.callback, **ctx.params)
File "c:\users\aka\dev\dream-team\env\lib\site-packages\click\core.py", line 535, in invoke
return callback(*args, **kwargs)
File "c:\users\aka\dev\dream-team\env\lib\site-packages\click\decorators.py", line 17, in new_func
return f(get_current_context(), *args, **kwargs)
File "c:\users\aka\dev\dream-team\env\lib\site-packages\flask\cli.py", line 257, in decorator
return __ctx.invoke(f, *args, **kwargs)
File "c:\users\aka\dev\dream-team\env\lib\site-packages\click\core.py", line 535, in invoke
return callback(*args, **kwargs)
File "c:\users\aka\dev\dream-team\env\lib\site-packages\flask_migrate\cli.py", line 90, in migrate
rev_id, x_arg)
File "c:\users\aka\dev\dream-team\env\lib\site-packages\flask_migrate\__init__.py", line 197, in migrate
version_path=version_path, rev_id=rev_id)
File "c:\users\aka\dev\dream-team\env\lib\site-packages\alembic\command.py", line 176, in revision
script_directory.run_env()
File "c:\users\aka\dev\dream-team\env\lib\site-packages\alembic\script\base.py", line 427, in run_env
util.load_python_file(self.dir, 'env.py')
File "c:\users\aka\dev\dream-team\env\lib\site-packages\alembic\util\pyfiles.py", line 81, in load_python_file
module = load_module_py(module_id, path)
File "c:\users\aka\dev\dream-team\env\lib\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 87, in <module>
run_migrations_online()
File "migrations\env.py", line 70, in run_migrations_online
poolclass=pool.NullPool)
File "c:\users\aka\dev\dream-team\env\lib\site-packages\sqlalchemy\engine\__init__.py", line 465, in engine_from_config
return create_engine(url, **options)
File "c:\users\aka\dev\dream-team\env\lib\site-packages\sqlalchemy\engine\__init__.py", line 424, in create_engine
return strategy.create(*args, **kwargs)
File "c:\users\aka\dev\dream-team\env\lib\site-packages\sqlalchemy\engine\strategies.py", line 50, in create
u = url.make_url(name_or_url)
File "c:\users\aka\dev\dream-team\env\lib\site-packages\sqlalchemy\engine\url.py", line 211, in make_url
return _parse_rfc1738_args(name_or_url)
File "c:\users\aka\dev\dream-team\env\lib\site-packages\sqlalchemy\engine\url.py", line 270, in _parse_rfc1738_args
"Could not parse rfc1738 URL from string '%s'" % name)
sqlalchemy.exc.ArgumentError: Could not parse rfc1738 URL from string 'mysql/dt_admin:dt2016@localhost/dreamteam_db'
答案 0 :(得分:5)
您没有在连接字符串中使用有效的URL。
查看有关如何构建MySQL连接URL的文档:http://docs.sqlalchemy.org/en/latest/dialects/mysql.html。
根据您使用的MySQL驱动程序,连接URL不同。例如,如果您使用pymysql,则您的URL应为:
mysql+pymysql://<username>:<password>@<host>/<dbname>[?<options>]
答案 1 :(得分:2)
我忘记了进入端口的端口号,这是URL连接字符串:
omission
现在有效,谢谢
答案 2 :(得分:0)
确保 URI 字符串中没有空格或换行符
答案 3 :(得分:0)
连接字符串中的URL无效。
您可以在此处查看有关如何构建MySQL连接URL的文档:http://docs.sqlalchemy.org/en/latest/dialects/mysql.html。
带有psycopg2驱动程序的postgresql的示例语法如下:-
sql_alchemy_conn = postgresql+psycopg2://ubuntu@localhost:5432/airflow