我正在尝试在 Heroku 上运行一个相当简单的 Flask + SQLAlchemy 站点,但我不确定应该如何运行迁移来设置我的数据库。当我运行时heroku run alembic upgrade head
,我收到以下错误:
Running `alembic upgrade head` attached to terminal... up, run.1
Traceback (most recent call last):
File "/app/.heroku/venv/bin/alembic", line 12, in <module>
load_entry_point('alembic==0.4.0', 'console_scripts', 'alembic')()
File "/app/.heroku/venv/lib/python2.7/site-packages/alembic/config.py", line 255, in main
CommandLine(prog=prog).main(argv=argv)
File "/app/.heroku/venv/lib/python2.7/site-packages/alembic/config.py", line 250, in main
self.run_cmd(cfg, options)
File "/app/.heroku/venv/lib/python2.7/site-packages/alembic/config.py", line 241, in run_cmd
**dict((k, getattr(options, k)) for k in kwarg)
File "/app/.heroku/venv/lib/python2.7/site-packages/alembic/command.py", line 124, in upgrade
script.run_env()
File "/app/.heroku/venv/lib/python2.7/site-packages/alembic/script.py", line 191, in run_env
util.load_python_file(self.dir, 'env.py')
File "/app/.heroku/venv/lib/python2.7/site-packages/alembic/util.py", line 185, in load_python_file
module = imp.load_source(module_id, path, open(path, 'rb'))
File "alembic/env.py", line 80, in <module>
run_migrations_online()
File "alembic/env.py", line 63, in run_migrations_online
poolclass=pool.NullPool)
File "/app/.heroku/venv/lib/python2.7/site-packages/sqlalchemy/engine/__init__.py", line 349, in engine_from_config
return create_engine(url, **opts)
File "/app/.heroku/venv/lib/python2.7/site-packages/sqlalchemy/engine/__init__.py", line 330, in create_engine
return strategy.create(*args, **kwargs)
File "/app/.heroku/venv/lib/python2.7/site-packages/sqlalchemy/engine/strategies.py", line 64, in create
dbapi = dialect_cls.dbapi(**dbapi_args)
File "/app/.heroku/venv/lib/python2.7/site-packages/sqlalchemy/dialects/sqlite/pysqlite.py", line 289, in dbapi
对我来说,这似乎表明它正在尝试加载 sqlite 的东西(这是我在 中的默认值alembic.ini
),但我有以下内容env.py
以使其使用 Heroku PostgreSQL 连接:
cur_db_uri = config.get_section_option('alembic', 'sqlalchemy.url')
my_db_uri = app.config.get('SQLALCHEMY_DATABASE_URI', cur_db_uri)
config.set_section_option('alembic', 'sqlalchemy.url', my_db_uri)
app
Flask 实例在哪里。我正在使用 Flask-SQLAlchemy 来干燥我在应用程序中的数据库使用情况,并使用 Flask-Heroku 来确保我的所有 Flask 配置变量都从 Heroku 环境变量中正确提取。