【问题标题】:Python flask with multi modules application OperationalError: (sqlite3.OperationalError) no such table具有多模块应用程序 OperationalError 的 Python 烧瓶:(sqlite3.OperationalError)没有这样的表
【发布时间】:2020-03-07 14:46:08
【问题描述】:

我最近一直在学习 python 并练习了几个教程并且它运行良好,直到我将我的应用程序划分为子模块以便更容易使用蓝图进行维护,这样做之后应用程序崩溃并抛出以下错误: OperationalError: (sqlite3.OperationalError) 没有这样的表,这是我如何划分我的应用程序:

-instance
  -flask.cfg
-app
  - static  // Folder for css
  - templates // Folder for common html files
  -__init__.py // to init the app 
  -module1
    -__init__.py // to register module blueprint
    -subModule1
      -subModule1 Group1
        -templates
          -htmls files
        -__init__.py file //to register group1 blueprint
        -Group1Form
        -Group1Model
        -Group1View
      -subModule1 Group2
        -templates
          -htmls files
        -__init__.py file //to register group2 blueprint
        -Group2Form
        -Group2Model
        -Group2View
    -subModule2
        ....
-main.py

当我使用flask run 时应用程序运行良好,但是当我使用db.session.add() 方法时出现问题,除了找不到表。我已经尝试了很多解决这个问题的方法,但没有运气。这是同一问题的一些链接:

OperationalError: (sqlite3.OperationalError) no such table: user

https://flask-sqlalchemy.palletsprojects.com/en/2.x/quickstart/

Python sqlite3.OperationalError: no such table:

'No application found. Either work inside a view function or push an application context.'

这是 flask.cfg 文件:

import os

BASEDIR = os.path.abspath(os.path.dirname(__file__))
SECRET_KEY = os.urandom(32)
DEBUG = True
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.abspath('app.db')

这是我在主 __init__.py 文件中初始化应用程序的方式:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()

def create_app(config_filename=None):
    app = Flask(__name__, instance_relative_config=True)
    app.config.from_pyfile(config_filename)
    initialize_extensions(app)
    register_blueprints(app)

def create_app(config_filename=None):
    app = Flask(__name__, instance_relative_config=True)
    app.config.from_pyfile(config_filename)
    initialize_extensions(app)
    register_blueprints(app)
    return app

def initialize_extensions(app):
    db.init_app(app)
    with app.app_context():
        db.create_all()

def register_blueprints(app):
    from app.module1.config.university import university_blueprint
    app.register_blueprint(university_blueprint)

    from app.module1.config.department import department_blueprint
    app.register_blueprint(department_blueprint)

    from app.module1.authors.author import author_blueprint
    app.register_blueprint(author_blueprint)

    from app.module1.authors.publications import    publications_blueprint
    app.register_blueprint(publications_blueprint)

【问题讨论】:

    标签: python flask flask-sqlalchemy blueprint


    【解决方案1】:

    你能在 app.app_context() 函数中移动 db.init_app() 吗

        with app.app_context():
            db.init_app()
    

    【讨论】:

    • 您已经正确启动了您的数据库,并且您的项目结构中有 .db 文件
    • 我在注册我的蓝图后通过添加 app.app_context(): db.create_all() 解决了这个问题
    【解决方案2】:

    我想出了问题,我应该放置:

    with app.app_context(): db.create_all()

    注册蓝图后

    【讨论】:

      猜你喜欢
      • 2016-02-20
      • 1970-01-01
      • 2015-07-30
      • 2021-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-12
      • 1970-01-01
      相关资源
      最近更新 更多