WebFaction
Community site: login faq

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 'table goes here' doesn't exist")

Some background information: I created the MySQL database on webfaction, but currently it does not have any tables within it. I assumed that when you're using peewee you can create tables from within your program like so:

# -- 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([example_table],safe=True)
    DATABASE.close()

So I didn't bother adding anything to my empty MySQL database on my webfaction control panel, but I'm just confused as to why I received the error above. Can anyone step me through successfully connecting to my MySQL database on webfaction?

Regards, Guled

EDIT:

Thank you Sean for your insight. I have included the exact errors from my error logs. I removed the timestamps 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")

asked 01 Dec '15, 22:18

Guled
1538
accept rate: 100%

edited 01 Dec '15, 23:28


Making a successful connection is exactly as you've shown in your example:

from peewee import *

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

The error you've presented doesn't seem to be a connection error at all.

If you can amend your question to include the full traceback leading up to the error, we might be able to assist further.

permanent link

answered 01 Dec '15, 22:31

seanf
12.2k31836
accept rate: 37%

The question has now been edited and it includes the error log. Thank you for the heads up Sean.

(01 Dec '15, 23:29) Guled

Based on that, I'd say the problem is simply that your initialize() function has never been called, so your tables haven't been created.

(01 Dec '15, 23:49) seanf

I made sure that I called the function in my init.py file like so:

if __name__ == "__main__": models.initialize() app.run()

I have a models.py file and I imported 'models' into my init.py file. I don't think that's the case.

(02 Dec '15, 00:33) Guled

Also, I'd like to mention that I never created the tables manually on webfaction (PHPmyadmin).

(02 Dec '15, 00:37) Guled

Your "if __name__ == "__main__":" block only runs if you run that specific script directly at the command line, eg: python2.7 __init__.py. It doesn't run when you simply import the module.

(02 Dec '15, 00:59) seanf

Sean, whenever you have time, please step me through this problem. I'm confused since I've developed my flask app, in the beginning, using nothing but a terminal and now that I'm moving an actual app and making it live, I just don't know where to start.

(02 Dec '15, 05:05) Guled

Do you mean that, if you start the application explicitly from the terminal, then it works - but if it's started by Apache (through mod_wsgi), then it doesn't? If that's true, then Sean's explanation makes sense; it indicates that you're only executing the initialization when _name_ == '__main__'. We can't step you through the problem because we don't know where the problem is - you need to debug this yourself - but Sean's comment sounds like the right place to start.

(02 Dec '15, 08:36) ryans ♦♦

Hey Ryan, thank you for the response. I think I have a good idea of who this works now. I followed your tutorial earlier on how to setup a simple flask app onto a webapp created on Webfaction, and I think I noticed something. So we know that name == 'main' doesn't run the application, I believe that wsgi.py is the file that runs the application ( correct me if I'm wrong). I'm trying to identify which file runs the flask app because I only want to run my models.py initialization code to create database tables only once. I'll keep debugging and let you know how it goes. Thank you both for the insight.

(02 Dec '15, 16:48) Guled

Hey Ryan, it seemed to work when I moved the database initialization code, while also importing the models.py, in wsgi.py. Which I believe is the file that runs my application. This file was based off your tutorial on deploying a flask app.

(02 Dec '15, 17:09) Guled

I still have some errors to fix here and there due to deprecations of some libraries of mine, but I'm very happy this worked out. Thank you Ryan and Sean!

(02 Dec '15, 17:10) Guled
showing 5 of 10 show 5 more comments
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×337
×113
×84
×55
×44

question asked: 01 Dec '15, 22:18

question was seen: 2,705 times

last updated: 02 Dec '15, 17:10

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2019 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM