python - 如何在 Pony ORM 中创建/连接 postgres 数据库?

标签 python database postgresql heroku ponyorm

我正在尝试创建/连接一个 postgres 数据库到我正在做的 Pony ORM 网络应用程序。起初我使用带有 Pony ORM 的 sqlite 数据库并且一切正常但我需要切换到 postgres 因为我想把它放在 Heroku 上。我使用了图形化的 postgres 工具并创建了一个名为“notice_db”的数据库和一个名为 notice 的 postgres 用户。我用小马绑定(bind)数据库的代码是:

db = Database()                                                                     
db.bind('postgres', user='notice', password='Notice',host='localhost', database='notice_db', dbname='notice_db', port='5432')

它会找到用户并连接到本地主机,但不会连接或创建任何数据库,所以当我尝试使用 Pony ORM 函数时,比如为我的用户类创建一个数据库实体,它具有属性“用户名”,我得到这个错误:“编程错误:列“用户名”不存在第 1 行:从用户中选择 *,其中用户名 = 'user1'”。

Pony 不连接或创建 postgres 数据库。所以我想知道如何将 postgres 数据库连接到 Pony ORM 应用程序?

最佳答案

为什么要定义 dbname 和 database? 参数“dbname”不适用于来自 pony 的 postgres 连接。 使用“数据库”而不是“数据库名称”:

db.bind('postgres', user='notice', password='Notice',
         host='localhost', database='notice_db', port='5432')

关于python - 如何在 Pony ORM 中创建/连接 postgres 数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35069600/

相关文章:

php - 使用MySql数据库用php登录

sql - 过滤多对多 (PostgreSQL)

sql - Postgres 唯一约束与索引

python - django-tinymce HTMLField 覆盖 admin/add 中的模型字段描述

python - 类型错误 : unsupported operand type(s) for -: 'str' and 'int' Writing a story?

python - 在Python中检查文件是否被修改

python - Python 中的 "result.status_code == 200"是什么意思?

mysql - 如何在连接未找到任何现有值的情况下显示 NULL 值?

database - m对n关系表的主键或外键

mysql - django mod_wsgi、nginx MySQL 或 Postgres