当我尝试运行Flask App时,出现以下消息:[这都是在尝试注释和取消注释db.create_all()命令之后,都在下面看到的内容]
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such
table: posts
这是我的app.py:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate, MigrateCommand
import os
basedir = os.path.abspath(os.path.dirname(__file__))
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' +
os.path.join(basedir, 'data.sqlite')
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
app.config.update(dict(
SECRET_KEY="powerful secretkey",
WTF_CSRF_SECRET_KEY="a csrf secret key"
))
db = SQLAlchemy(app)
migrate = Migrate(app, db)
#db.create_all()
这是我的模型。py:
from flask_mongoengine.wtf import model_form
from mongoengine import Document, StringField, EmailField,
ReferenceField, IntField, DateTimeField, \
BooleanField, DictField, ListField, EmbeddedDocument, \
EmbeddedDocumentListField, FloatField, FileField
from datetime import datetime
from werkzeug.security import generate_password_hash,
check_password_hash
from flask import request
from hashlib import md5
from app import db
#from app import mongo
class User(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key = True)
email = db.Column(db.String(64), unique=True, index=True)
username = db.Column(db.String(64), unique=True, index=True)
password = db.Column(db.String(128))
confirmed = db.Column(db.Boolean, default=False)
posts = db.relationship('Post', backref = 'author', lazy='dynamic')
class Post(db.Model):
__tablename__ = 'posts'
id=db.Column(db.Integer, primary_key = True)
body = db.column(db.Text)
timestamp = db.Column(db.DateTime, index=True,
default=datetime.utcnow)
author_id = db.Column(db.Integer, db.ForeignKey('users.id'))
这是我的views.py:
import bcrypt
#import magic
from flask import Flask, request, render_template, flash, redirect,
session, url_for, g, jsonify, make_response
from werkzeug.datastructures import CombinedMultiDict
from werkzeug.utils import secure_filename
from app import app
from app import db
from models import User, Post
from forms import RegisterForm, LoginForm, ForgotForm, ProfileForm,
ResetPassword, InviteForm, PostForm
from flask_mongoengine import MongoEngine
from flask_login import login_user, logout_user, current_user,
login_required, LoginManager
import os
from flask_babel import Babel, lazy_gettext
from flask_uploads import UploadSet, IMAGES, configure_uploads
from flask_avatar import Avatar
from momentjs import momentjs
from flask_mail import Mail
@app.route('/index', methods=['GET', 'POST'])
#@app.route('/index/<int:page>', methods=['GET', 'POST'])
def index():
form = PostForm()
if form.validate_on_submit():
post = Post(body=form.body.data, author =
current_user._get_current_object())
db.session.add(post)
return redirect(url_for('frontpage'))
posts = Post.query.order_by(Post.timestamp.desc()).all()
return render_template('frontpage.html', form=form, posts=posts)
我认为错误是,尽管定义了模型,但实际上并未在数据库中创建与模型匹配的表..?当我尝试在终端中执行以下操作时:
python models.py shell
我收到以下消息:
sqlalchemy.exc.InvalidRequestError: Table 'users' is already defined for
this MetaData instance. Specify 'extend_existing=True' to redefine
options and columns on an existing Table object.
据我了解,如果以上命令成功运行,则可以在终端中运行以下命令:
from models import db
db.create_all()
这将创建我尝试制作的表格帖子...
任何帮助将不胜感激。我想我在这里想念什么...