Mah*_*ala 76 python postgresql sqlalchemy psycopg2 flask-sqlalchemy
我正在尝试使用 SQLAlchemy 连接到 Postgres 数据库。我已经安装了 psycopg2。但是,我收到错误sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:postgres。如何配置 SQLAlchemy 以连接到 PostgreSQL?
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = "postgres://username@localhost:5432/template1"
db = SQLAlchemy(app)
Run Code Online (Sandbox Code Playgroud)
Lea*_*ima 118
URI 应该以postgresql://而不是开头postgres://。SQLAlchemy 过去接受两者,但已取消对postgres名称的支持。
Git*_*son 70
SQLAlchemy 1.4 删除了不推荐使用的postgres方言名称,postgresql现在必须改用该名称。方言是://URL 中的之前的部分。SQLAlchemy 1.3 及更早版本显示了弃用警告,但仍接受它。
要解决此问题,postgres://请将 URL重命名为postgresql://.
这个错误目前在使用 Heroku 时出现,它postgres在DATABASE_URL他们提供的SQLALCHEMY_DATABASE_URI. 要在他们更新之前解决此问题,请将 Heroku 仪表板中的变量更新为使用postgresql.
小智 28
heroku 中的问题已通过使用简单的 python url 替换代码得到解决
import os
import re
uri = os.getenv("DATABASE_URL") # or other relevant config var
if uri and uri.startswith("postgres://"):
uri = uri.replace("postgres://", "postgresql://", 1)
# rest of connection code using the connection string `uri`
Run Code Online (Sandbox Code Playgroud)
来源:https://help.heroku.com/ZKNTJQSK/why-is-sqlalchemy-1-4-x-not-connecting-to-heroku-postgres
Sou*_*ETO 10
要解决此问题,请将postgres://URL 重命名为postgresql+psycopg2://.
| 归档时间: |
|
| 查看次数: |
29249 次 |
| 最近记录: |