0

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

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

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

一些背景资料:

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

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

  • 我可以通过终端成功运行我的烧瓶应用程序,但我即将让它在 Web 服务器上运行,所以我对这个过程非常陌生。

我假设当您使用 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文件中路由到 ('/') 的索引视图调用 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")

谁能帮我识别和解决这个问题?我不知道如何让我的烧瓶应用程序与 webfaction 上的 MySQL 数据库合作。

4

2 回答 2

2

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

于 2015-12-02T04:58:25.727 回答
1

将我的服务器初始化代码移动到在 Webfaction 上运行 Flask 应用程序的文件中。不建议在条件下放置额外的代码,因为它意味着命令行。

于 2015-12-02T19:01:02.443 回答