Search code examples
mysqlflaskpeeweewebfactionflask-peewee

How do I connect to MySQL database on WebFaction using Peewee ORM?


I successfully uploaded my webapp onto webfaction, but I noticed, when using peewee to connect to a MySQL database on my webfaction account, I got this error:

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

Exact error is in the error log file below

Some background information:

  • I created a MySQL database on webfaction

  • I did not create any tables within the control panel provided by the service. It's completely empty.

  • I can run my flask app successfully through the terminal, but I am about to make it live on a web server, and so I am very new to this process.

I assumed that when you're using peewee you can create tables from within your program like so:

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()

The initialize function is called in the __init__.py file at the bottom of the file like so:

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()

My index view, which routes to ('/'), in my __init__.py file makes a call to the count method like so:

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

And I believe this line caused my site to display a 500 internal server error which resulted in this error log (Timestamps have been removed for simplicity):

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")

Can anyone help me identify and fix this issue? I have no idea how to get my flask app to cooperate with my MySQL database on webfaction.


Solution

  • Moved my server initialization code into a file that runs the Flask app on Webfaction. It is not recommended to place additional code under the conditional as its meant for the command line.