Flask-SQLAlchemy OperationalError: (sqlite3.OperationalError) no such table
myapp.py
from flask import Flask
app = Flask(__name__)
app.config.from_object('config')
from view import *
if __name__ == '__main__':
app.run(debug=True)
view.py
from flask import render_template
from myapp import app
from model import db,User
@app.route('/test')
def test():
aaa=User(username='admin',password='admin',email='xxx')
db.session.add(aaa)
db.session.commit()
return 'commit user'
config.py
SQLALCHEMY_DATABASE_URL="mysql://%s:%s@%s/%s" % ('root', 'root', '127.0.0.1:3306', 'blog')
model.py
from flask.ext.sqlalchemy import SQLAlchemy
from myapp import app
db=SQLAlchemy(app)
class User(db.Model):
__tablename__='user'
id=db.Column(db.Integer,primary_key=True,unique=True)
username=db.Column(db.String(45))
password=db.Column(db.String(45))
email=db.Column(db.String(45))
def __repr__(self):
return '<User %r>' % self.username
run myapp.py and go to /test
:
OperationalError: (sqlite3.OperationalError) no such table user
I have already created a database and a table
+3
source to share
No one has answered this question yet
Check out similar questions: