I am trying to get Flask-SQLAlchemy working and running in to some hiccups. Take a look at the two files I'm using. When I run gwg.py
and goto /util/db/create-all
it spits out an error no such table: stories
. I thought I did everything correct; can someone point out what I'm missing or whats wrong? It does create data.db but the file shows as 0Kb
gwg.py:
application = Flask(__name__)
db = SQLAlchemy(application)
import models
# Utility
util = Blueprint('util', __name__, url_prefix='/util')
@util.route('/db/create-all/')
def db_create_all():
db.create_all()
return 'Tables created'
application.register_blueprint(util)
application.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db'
application.debug = True
application.run()
models.py:
from gwg import application, db
class Story(db.Model):
__tablename__ = 'stories'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(150))
subscribed = db.Column(db.Boolean)
def __init__(self, name):
self.name = name
self.subscribed = False
def toggle_subscription(self):
self.subscribed = False if self.subscribed else True
Here is the function that seems to be triggering the error but it shouldn't because I specifically go to /util/db/create-all first and then reload /. Even after the error I go to /util/db/create-all and then / and still get the same error
@application.route('/')
def homepage():
stories = models.Story.query.all()
return render_template('index.html', stories=stories)
Stacktrace
sqlalchemy.exc.OperationalError
OperationalError: (OperationalError) no such table: stories u'SELECT stories.id AS stories_id, stories.name AS stories_name, stories.subscribed AS stories_subscribed \nFROM stories' ()
Traceback (most recent call last)
File "C:\Python27\lib\site-packages\flask\app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "C:\Python27\lib\site-packages\flask\app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "C:\Python27\lib\site-packages\flask\app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "C:\Python27\lib\site-packages\flask\app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "C:\Python27\lib\site-packages\flask\app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Python27\lib\site-packages\flask\app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Python27\lib\site-packages\flask\app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Python27\lib\site-packages\flask\app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "C:\Users\kylee\Code\GWG\gwg.py", line 20, in homepage
stories = models.Story.query.all()
File "C:\Python27\lib\site-packages\sqlalchemy\orm\query.py", line 2104, in all
return list(self)
File "C:\Python27\lib\site-packages\sqlalchemy\orm\query.py", line 2216, in __iter__
return self._execute_and_instances(context)
File "C:\Python27\lib\site-packages\sqlalchemy\orm\query.py", line 2231, in _execute_and_instances
result = conn.execute(querycontext.statement, self._params)
File "C:\Python27\lib\site-packages\sqlalchemy\engine\base.py", line 662, in execute
params)
File "C:\Python27\lib\site-packages\sqlalchemy\engine\base.py", line 761, in _execute_clauseelement
compiled_sql, distilled_params
File "C:\Python27\lib\site-packages\sqlalchemy\engine\base.py", line 874, in _execute_context
context)
File "C:\Python27\lib\site-packages\sqlalchemy\engine\base.py", line 1024, in _handle_dbapi_exception
exc_info
File "C:\Python27\lib\site-packages\sqlalchemy\util\compat.py", line 163, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb)
File "C:\Python27\lib\site-packages\sqlalchemy\engine\base.py", line 867, in _execute_context
context)
File "C:\Python27\lib\site-packages\sqlalchemy\engine\default.py", line 324, in do_execute
cursor.execute(statement, parameters)
OperationalError: (OperationalError) no such table: stories u'SELECT stories.id AS stories_id, stories.name AS stories_name, stories.subscribed AS stories_subscribed \nFROM stories' ()
The debugger caught an exception in your WSGI application. You can now look at the traceback which led to the error.
To switch between the interactive traceback and the plaintext one, you can click on the "Traceback" headline. From the text traceback you can also create a paste of it. For code execution mouse-over the frame you want to debug and click on the console icon on the right side.
You can execute arbitrary Python code in the stack frames and there are some extra helpers available for introspection:
dump() shows all variables in the frame
dump(obj) dumps all that's known about the object
Brought to you by DON'T PANIC, your friendly Werkzeug powered traceback interpreter.
The issue here is one a gotcha in the Python import system. It can be simplified to the following explanation...
Assume you have two files in a directory...
a.py:
print 'a.py is currently running as module {0}'.format(__name__)
import b
print 'a.py as module {0} is done'.format(__name__)
b.py:
print 'b.py is running in module {0}'.format(__name__)
import a
print 'b.py as module {0} is done'.format(__name__)
The results of running python a.py
are the following...
a.py is currently running as module __main__
b.py is running in module b
a.py is currently running as module a
a.py as module a is done
b.py as module b is done
a.py as module __main__ is done
Notice that when a.py
is run, the module is called __main__
.
Now think about the code you have. In it, your a.py
script creates your application
and db
objects. However, those values are not stored in a module called a
, they are stored in a module called __main__
. Therefore, b.py
tries to import a
, it is NOT importing the values that you just created a few seconds ago! Instead, since it doesn't find module a
, it creates a NEW module, running a.py
a SECOND TIME and storing the results into the module a
.
You can use print statements like what I have shown above to hopefully guide you through the entire process to see exactly what's going on. The solution is to make sure that you are only calling a.py
ONCE, and when b.py
imports a
it will import the same values as a.py
rather than importing it a second time.
The easiest way to fix this would be to create a python script that is just for running the application. Remove the application.run()
from the end of gwg.py, and add the following script...
main.py:
from gwg import application
application.run()
Then run using python main.py
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With