如何使用Peewee ORM在WebFaction上连接MySQL数据库?

时间:2015-12-02 04:50:42

标签: mysql flask peewee webfaction flask-peewee

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

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

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

一些背景信息:

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

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

  • 我可以通过终端成功运行我的烧瓶应用程序,但我即将在网络服务器上运行,所以我对这个过程很新。

我认为当你使用peewee时,你可以在你的程序中创建表格,如下所示:

models.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数据库合作。

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

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