烧瓶sqlalchemy.exc.OperationalError:(OperationalError)尝试使用SQLAlchemy更新数据库时没有这样的表 [英] Flask sqlalchemy.exc.OperationalError: (OperationalError) no such table when trying to update database with SQLAlchemy

查看:359
本文介绍了烧瓶sqlalchemy.exc.OperationalError:(OperationalError)尝试使用SQLAlchemy更新数据库时没有这样的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Flask的新手,在设置非常基本的Flask应用程序时遇到了一些问题. 现在,我正在尝试让用户在主页上提交表单,然后将该表单保存到SQL数据库.

I'm new to Flask, and having some issues setting up a very basic Flask app. Right now, I'm trying to get the user to submit a form on the homepage and then save that form to a SQL database.

但是,当用户提交表单时,出现以下错误

However, when the user submits the form, I get the following error

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: contact [SQL: 'INSERT INTO contact (name, email) VALUES (?, ?)'] [parameters: ('aaa', 'aaa')] (Background on this error at: http://sqlalche.me/e/e3q8)

相关代码如下:

app/models.py

app/models.py

from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField, BooleanField, SubmitField
from wtforms.validators import DataRequired

class ContactForm(FlaskForm):
    name = StringField('Name', validators=[DataRequired()])
    email = PasswordField('Email', validators=[DataRequired()])
    submit = SubmitField('submit')

app/__init__ .py

from flask import Flask
from config import Config
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
migrate = Migrate(app, db)
from app import routes, models

app/config.py

app/config.py

import os
basedir = os.path.abspath(os.path.dirname(__file__))


class Config(object):
    SECRET_KEY = os.environ.get('SECRET_KEY') or 'some-secret-key'
    SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or 'sqlite:///' + os.path.join(basedir, 'app.db')
    SQLALCHEMY_TRACK_MODIFICATIONS = False
    enter code here

我在哪里哪里出问题了?我是否必须在某个地方调用db.create_all()?如果是这样,在哪里?

Where could I be going wrong here? Do I have to call db.create_all() somewhere? If so, where?

谢谢!

我也已经运行

flask db init
flask db migrate
flask db upgrade

推荐答案

在models.py中创建模型:

Create a model in models.py:

from __init__.py import db

class ContactModel(db.Model):
    __tablename__ = 'contact'

    id = db.Column(db.Integer, primary_key = True)
    name = db.Column(db.String(120))
    email = db.Column(db.String(120))

    def save_to_db(self):
        db.session.add(self)
        db.session.commit()

然后在 init .py中输入

from flask import Flask
from config import Config
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
migrate = Migrate(app, db)

#create all db tables
@app.before_first_request
def create_tables():
    from models import ContactModel
    db.create_all()

from app import routes, models

这篇关于烧瓶sqlalchemy.exc.OperationalError:(OperationalError)尝试使用SQLAlchemy更新数据库时没有这样的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆