【问题标题】:Flask and Heroku sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:postgresFlask 和 Heroku sqlalchemy.exc.NoSuchModuleError:无法加载插件:sqlalchemy.dialects:postgres
【发布时间】:2021-06-15 19:24:05
【问题描述】:

当我跑步时

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()

它给出了这个错误:

  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

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

(大多数其他问题like this one 是拼写错误或无法解决此问题的错误。)

【问题讨论】:

标签: python postgresql flask heroku sqlalchemy


【解决方案1】:

这是由于 sqlalchemy 库中的更改。这是将方言(SQLALCHEMY_DATABASE_URI 中“:”之前的部分)名称postgres 更改为postgresql 时宣布的弃用。他们通过 minor version release 发布了此 github commit 的重大更改,这是他们这样做的政策。

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

与此同时,您可以将 sqlalchemy 库固定回

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

【讨论】:

    【解决方案2】:

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

    SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL').replace("://", "ql://", 1)
    

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

    【讨论】:

    • 可能值得做一个完整的:python SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL').replace("postgres://", "postgresql://", 1) ,这样如果 Heroku 更新了环境变量的值,这将继续工作
    猜你喜欢
    • 2020-10-22
    • 2023-02-05
    • 2015-02-21
    • 2015-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-02
    相关资源
    最近更新 更多