S
S
Secret_dungeon2020-10-12 20:22:12
Flask
Secret_dungeon, 2020-10-12 20:22:12

Sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table. How can I solve this problem?

How to solve this problem

from flask import Flask, render_template, url_for,request,redirect
from flask_sqlalchemy import SQLAlchemy
from datetime import datetime


app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = 'sqlite:///blog.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_BINDS'] = {
    'messages': 'sqlite:///message.db'
}
db = SQLAlchemy(app)







class Article(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(100), nullable=False)
    intro = db.Column(db.String(300), nullable=False)
    text = db.Column(db.Text, nullable=False)
    date = db.Column(db.DateTime, default=datetime.utcnow)



    def __repr__(self):
        return '<Article %r>' % self.id




class Message(db.Model):

    __bind_key__ = 'messages'

    id = db.Column(db.Integer, primary_key=True)
    Name = db.Column(db.Text, nullable=False)
    Surname = db.Column(db.Text, nullable=False)
    text = db.Column(db.Text, nullable=False)
    date = db.Column(db.DateTime, default=datetime.utcnow)


    def __repr__(self):
        return '<Message %r>' % self.Name



@app.route('/timetable')
def timetable():
    return render_template('timetable.html')


@app.route('/')
def index():

    articles = Article.query.order_by(Article.date.desc()).all()
    return render_template('index.html', articles=articles)




@app.route('/about')
def about():
    return render_template('about.html')


@app.route('/posts')
def posts():
    articles = Article.query.order_by(Article.date.desc()).all()
    return render_template('posts.html', articles=articles)


@app.route('/posts/<int:id>')
def posts_detail(id):
    article = Article.query.get(id)
    return render_template('posts-detail.html', article=article)


@app.route('/posts/<int:id>/del')
def posts_delete(id):
    article = Article.query.get_or_404(id)
    try:
        db.session.delete(article)
        db.session.commit()
        return redirect("/posts")

    except:
        return "При удалении статьи произошла ошибка"

@app.route('/message/<int:id>/del')
def message_delete(id):

    message = Message.query.get_or_404(id)
    try:
        db.session.delete(message)
        db.session.commit()
        return redirect("/message")

    except:
        return "При удалении статьи произошла ошибка"


@app.route('/message/<int:id>/update', methods=["POST", 'GET'])
def message_update(id):
    message = Message.query.get(id)

    if request.method == "POST":
        message.Name = request.form['Name']
        message.Surname = request.form['Surname']
        message.text = request.form['text']


        try:
            db.session.commit()
            return redirect("/message")
        except:
            return "При добавлении статьи произошла ошибка"

    else:

        return render_template("message_update.html", message=message)


@app.route('/posts/<int:id>/update', methods=["POST", 'GET'])
def create_update(id):
    article = Article.query.get(id)

    if request.method == "POST":
        article.title = request.form['title']
        article.intro = request.form['intro']
        article.text = request.form['text']


        try:
            db.session.commit()
            return redirect("/posts")
        except:
            return "При добавлении статьи произошла ошибка"

    else:

        return render_template("post_update.html", article=article)


@app.route('/create-article', methods=["POST", 'GET'])
def create_article():
    if request.method == "POST":
        title = request.form['title']
        intro = request.form['intro']
        text = request.form['text']
        article = Article(title=title, intro=intro, text=text)

        try:
            db.session.add(article)
            db.session.commit()
            return redirect("/posts")
        except:
            return "При добавлении статьи произошла ошибка"

    else:
        return render_template("create-article.html")


@app.route('/message', methods=["POST", 'GET'])
def message():
    if request.method == "POST":
        Name = request.form['Name']
        Surname = request.form['Surname']
        text = request.form['text']
        message = Message(Name=Name, Surname=Surname, text=text)

        try:
            db.session.add(message)
            db.session.commit()
            return redirect("/message")
        except:
            return "При добавлении статьи произошла ошибка"

    else:

        messages = Message.query.order_by(Message.date.desc()).all()
        return render_template('message.html', messages = messages)



if __name__ == "__main__":
    app.run(debug=True)

Here is the complete error sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: message
2020-10-12T15:53:59.543271+00:00 app[web.1]: [SQL: SELECT message.id AS message_id, message ."Name" AS "message_Name", message."Surname" AS "message_Surname", message.text AS message_text, message.date AS message_date
2020-10-12T15:53:59.543272+00:00 app[web.1]: FROM message ORDER BY message.date DESC]

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey S., 2020-10-12
@Winsik

Is there such a table?

message

Then you connected to message s

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question