【问题标题】:Tables created with Peewee seem to disappear into thin air用 Peewee 创建的表格似乎消失得无影无踪
【发布时间】:2014-01-23 07:12:37
【问题描述】:

我目前正在使用peewee orm for postgresql 编写一个烧瓶应用程序。过去它工作得很好,但似乎有些东西被打乱了,我不清楚如何准确地调试问题。

首先我像这样启动数据库:

/usr/local/pgsql/bin/psql -D /usr/local/pgsql/data

我创建一个新数据库:

/usr/local/pgsql/bin/createdb indico

像这样配置连接:

db = peewee.PostgresqlDatabase(
'indico',
host="/tmp/",
user = 'indico', # There is also an indico user, this is not a db name confusion
password = 'password',
)

创建所需的表:

class PostgresqlModel(peewee.Model):
    """A base model that will use our Postgresql database"""
    _id = peewee.PrimaryKeyField()

    class Meta:
        database = db
PostgresqlModel.create_table()

但是当我尝试实际访问创建的表时:

/usr/local/pgsql/bin/psql -d indico -U indico
>>> \dt
no relations found

似乎什么都没有出现。如果我尝试再次创建它们,我会收到一条错误消息,指出关系已经存在,虽然我能够保存对象,但我无法检索它们。

我觉得我在这里用我的路径和配置做了一些愚蠢的事情,但我似乎无法弄清楚,而且我无法找到关于我应该在这里做的正确内省的文档。不是在寻找神奇的解决方法,但如果有人能指出我正确的方向,那将不胜感激。

【问题讨论】:

    标签: python postgresql orm flask peewee


    【解决方案1】:

    原来这是peewee 的自动提交行为的子集。如果 autocommit 设置为 False,则创建的任何数据库都只能在活动数据库提交中访问。因此,如果您在导入模块时创建数据库。

    db.commit() 添加到create_table 序列的末尾似乎可以解决此问题。

    if __name__ == "__main__":
        User.create_table()
        db.commit() 
    

    【讨论】:

    • 当你实例化你的数据库时(在问题描述中)它看起来不像你设置了autocommit=False,所以我很困惑为什么自动提交会是假的。
    猜你喜欢
    • 2011-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-23
    • 1970-01-01
    • 1970-01-01
    • 2012-04-20
    相关资源
    最近更新 更多