Flask 和 Heroku sqlalchemy.exc.NoSuchModuleError:无法加载插件:sqlalchemy.dialects:postgres

hal*_*ier 10 python postgresql sqlalchemy heroku flask

当我跑

heroku run python
>>> from app.main import app
>>> app.config['SQLALCHEMY_DATABASE_URI']
'postgres://<url string>' # the database url is passed correctly
>>> from app.main import db
>>> db.create_all()
Run Code Online (Sandbox Code Playgroud)

它给出了这个错误:

  Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/app/.heroku/python/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 1039, in create_all
    self._execute_for_all_tables(app, bind, 'create_all')
  File "/app/.heroku/python/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 1031, in _execute_for_all_tables
    op(bind=self.get_engine(app, bind), **extra)
  File "/app/.heroku/python/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 962, in get_engine
    return connector.get_engine()
  File "/app/.heroku/python/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 556, in get_engine
    self._engine = rv = self._sa.create_engine(sa_url, options)
  File "/app/.heroku/python/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py", line 972, in create_engine
    return sqlalchemy.create_engine(sa_url, **engine_opts)
  File "<string>", line 2, in create_engine
  File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/util/deprecations.py", line 298, in warned
    return fn(*args, **kwargs)
  File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/engine/create.py", line 520, in create_engine
    entrypoint = u._get_entrypoint()
  File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/engine/url.py", line 653, in _get_entrypoint
    cls = registry.load(name)
  File "/app/.heroku/python/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 342, in load
    "Can't load plugin: %s:%s" % (self.group, name)
sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:postgres
Run Code Online (Sandbox Code Playgroud)

我很困惑,因为我是 Heroku 和 Postgresql 的新手(直到现在一直在使用 SQLite)而且我所遵循的教程都没有解释它是如何连接到 Flask 的,只有如何做到这一点。所以我不明白要解决什么问题。我应该在问题中包含任何其他代码吗?

像这样的大多数其他问题都是拼写错误或无法解决此问题的错误。)

Hoo*_*ddy 11

这是由于 sqlalchemy 库中的更改。这是在将方言(SQLALCHEMY_DATABASE_URI 中“:”之前的部分)名称postgres更改为postgresql. 他们从这个github commit 中发布了这个带有小版本 release 的重大更改,这是他们的政策。

Heroku 的默认方言postgres在他们提供的 DATABASE_URL 中,它被翻译成 SQLALCHEMY_DATABASE_URI。如果更新不会破坏可能依赖它的其他库,Heroku 可以更新他们的 postgres 附加组件。

同时,您可以将 sqlalchemy 库固定回 <1.4.0(1.3.23 是最后一个 1.3.x 版本),它应该可以工作。

或者,您可以更新代码以修改方言。


小智 11

这是一个对我有用的快速方法,在 Heroku 上有最新的 PostgreSQL:

SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL').replace("://", "ql://", 1)
Run Code Online (Sandbox Code Playgroud)

只需将postgres://Heroku(无法编辑)修改为postgresql://.

  • 可能值得做一个完整的: ```python SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL').replace("postgres://", "postgresql://", 1) ``` 这样才能继续如果 Heroku 更新环境变量的值,则可以工作 (4认同)