I am trying to use flask-sqlalchemy to manage my pre-existing mysql database, and I have a table named content. Here is my code snippet.
# coding:utf-8
from flask import Flask, render_template
from flask.ext.bootstrap import Bootstrap
from flask.ext.sqlalchemy import SQLAlchemy
...
app = Flask(__name__)
bootstrap = Bootstrap(app)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://...'
db = SQLAlchemy(app)
class Content(db.Model):
__table__ = db.Model.metadata.tables['content']
def __repr__(self):
return '<title %r>' % self.title
@app.route('/', methods=['GET', 'POST'])
def index():
return render_template('index.html')
...
if __name__ == '__main__':
app.run(debug=True)
When I run the code above, I get this error
Traceback (most recent call last):
File "hello.py", line 13, in <module>
class Content(db.Model):
File "hello.py", line 14, in Content
__table__ = db.Model.metadata.tables['content']
KeyError: 'content'
Any help will be appreciated.