Flask sqlalchemy.exc.OperationalError: (OperationalError) no such table when trying to update database with SQLAlchemy

后端 未结 3 639
心在旅途
心在旅途 2021-01-25 13:31

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

3条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-25 14:05

    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()
    

    Then in 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
    

提交回复
热议问题