Skip to main content
 首页 » 编程设计

python之Heroku:关系不存在

2025年05月04日11grandyang

我正在使用 Python、Flask 和 Peewee ORM。我的 DATABASE_URL 设置正确。

我已经在本地服务器上测试了该功能并且它工作正常。当我部署到 Heroku 时,Peewee 在我的应用程序的 if name == main 部分创建的表中出现错误。

错误是:

ProgrammingError: relation "connection" does not exist (connection  
is the name of my model/would-be table). 

更新:我通过在特定路径下的 views.py 文件中创建表来解决问题。这是必要的吗?我更喜欢更清洁的方式。

请您参考如下方法:

我从未使用过 Heroku,但我猜测 Heroku 正在以某种方式导入您的应用程序,从而绕过 if __name__ == "__main__" block ,该 block 仅在该模块运行时运行直接运行。您应该尝试将逻辑从 if block 移动到 before_first_request处理程序,以便它仍将运行,但仅运行一次(每个工作人员/应用程序实例)

@app.before_first_request 
def initialize(): 
    app.logger.info("Creating the tables we need") 
    ...