I'm using Flask with flask.ext.sqlalchemy for the first time. I'm getting an error telling me a table doesn't exist, but I thought the object relational setup would take care of that. Guess not. What am I missing in the following code?
from flask import Flask, request, url_for
from flask.ext.sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.secret_key = 'This is really unique and secret'
db = SQLAlchemy(app)
app.config['SQLALCHEMY_DATABASE_URI'] ='mysql://golfape:mypass@mysql.server/golfape$swarm'
class Suggestion(db.Model):
__tablename__ = 'suggestions'
id = db.Column(db.Integer, primary_key = True)
name = db.Column(db.String(100))
def __init__( self, name ):
self.name = name
@app.route('/')
def get_suggestion():
return """
<p>What do you think swarm prediction could be applied to?</p>
<form method = "POST" action="%s">
<input name="name" />
<input type="submit" value="Go!" />
</form>
""" % (url_for('append_suggestion'),)
@app.route('/suggestions', methods=['POST'] )
def append_suggestion():
name = request.form["name"]
newSuggestion = Suggestion( name = name )
db.session.add( newSuggestion )
db.session.commit()
return """
<p>Thanks for suggesting %s</p>
<p><a href="%s">Back</a></p>
""" % (name, url_for('get_suggestion'))
Here is the error:
File "/usr/local/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler raise errorclass, errorvalue ProgrammingError: (ProgrammingError) (1146, "Table 'golfape$swarm.suggestions' doesn't exist") 'INSERT INTO suggestions (name) VALUES (%s)' ('fishing',)