gpt4 book ai didi

mysql - 如何使用 Peewee ORM 连接到 WebFaction 上的 MySQL 数据库?

转载 作者:太空宇宙 更新时间:2023-11-03 11:54:03 24 4
gpt4 key购买 nike

我成功地将我的 webapp 上传到 webfaction,但我注意到,当使用 peewee 连接到我的 webfaction 帐户上的 MySQL 数据库时,我收到了这个错误:

ProgrammingError: (1146, "Table 'TABLEGOESHERE' doesn't exist")

确切的错误在下面的错误日志文件中

一些背景信息:

  • 我在webfaction上创建了一个MySQL数据库

  • 我没有在服务提供的控制面板中创建任何表格。它完全是空的。

  • 我可以通过终端成功运行我的 flask 应用程序,但我要让它在网络服务器上运行,所以我对这个过程还很陌生。

我假设当您使用 peewee 时,您可以像这样在您的程序中创建表格:

模型.py

# -- Peewe Modules
from peewee import *

DATABASE = MySQLDatabase("DBNAMEGOESHERE", host="HOSTGOESHERE", port=PORTGOESHERE, user="USERGOESHERE", passwd="PASSGOESHERE")

# -- DATABASE OBJECTS GO HERE:

#-- INIT
def initialize():
DATABASE.connect()
DATABASE.create_tables([Post, etc...],safe=True)
DATABASE.close()

初始化函数在文件底部的 __init__.py 文件中调用,如下所示:

if __name__ == "__main__":
models.initialize()
try:
models.User.create_user(
username = 'user',
email = 'email',
password = 'pass',
is_admin = True,
confirmed = True,
confirmed_on = datetime.datetime.now(),
)
except ValueError:
pass
app.run()

在我的 __init__.py 文件中,路由到 ('/') 的索引 View 调用了 count 方法,如下所示:

count = models.Post.select().count()

而且我相信这一行导致我的网站显示 500 内部服务器错误,从而导致此错误日志(为简单起见已删除时间戳):

return self.wsgi_app(environ, start_response)

File "/home/username/webapps/myapp/myapp/__init__.py", line 49, in __call__
return self.app(environ, start_response)

File "/home/username/lib/python2.7/Flask-0.10.1-py2.7.egg/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))

File "/home/username/lib/python2.7/Flask-0.10.1-py2.7.egg/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)

File "/home/username/lib/python2.7/Flask-0.10.1-py2.7.egg/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()

File "/home/username/lib/python2.7/Flask-0.10.1-py2.7.egg/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)

File "/home/username/lib/python2.7/Flask-0.10.1-py2.7.egg/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)

File "/home/username/lib/python2.7/Flask-0.10.1-py2.7.egg/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()

File "/home/username/lib/python2.7/Flask-0.10.1-py2.7.egg/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)

File "/home/username/webapps/myapp/myapp/__init__.py", line 587, in index
count = models.Post.select().count()

File "/home/username/lib/python2.7/peewee-2.7.3-py2.7.egg/peewee.py", line 2792, in count
return self.aggregate(convert=False) or 0

File "/home/username/lib/python2.7/peewee-2.7.3-py2.7.egg/peewee.py", line 2785, in aggregate
return self._aggregate(aggregation).scalar(convert=convert)

File "/home/username/lib/python2.7/peewee-2.7.3-py2.7.egg/peewee.py", line 2564, in scalar
row = self._execute().fetchone()

File "/home/username/lib/python2.7/peewee-2.7.3-py2.7.egg/peewee.py", line 2555, in _execute
return self.database.execute_sql(sql, params, self.require_commit)

File "/home/username/lib/python2.7/peewee-2.7.3-py2.7.egg/peewee.py", line 3366, in execute_sql
self.commit()

File "/home/username/lib/python2.7/peewee-2.7.3-py2.7.egg/peewee.py", line 3212, in __exit__
reraise(new_type, new_type(*exc_args), traceback)

File "/home/username/lib/python2.7/peewee-2.7.3-py2.7.egg/peewee.py", line 3359, in execute_sql
cursor.execute(sql, params or ())

File "/usr/lib64/python2.7/site-packages/MySQLdb/cursors.py", line 174, in execute
self.errorhandler(self, exc, value)

File "/usr/lib64/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
ProgrammingError: (1146, "Table 'DATABASENAMEHERE.post' doesn't exist")

谁能帮我找出并解决这个问题?我不知道如何让我的 flask 应用程序与我在 webfaction 上的 MySQL 数据库合作。

最佳答案

您确定您的应用程序正在通过直接从命令行执行来运行吗?即 __name__ == '__main__' block 实际上正在运行?您是否有可能改用专用的 WSGI 服务器?

关于mysql - 如何使用 Peewee ORM 连接到 WebFaction 上的 MySQL 数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34035164/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com