簡體   English   中英

當我運行我的應用程序時,Flask 拋出異常

[英]Flask throwing exception when i run my app

我開始了一個簡單的 Flask 項目,我正在使用 SLQAlchemy 來處理我的數據庫。 我的問題是每次我運行我的應用程序時,我都會收到以下錯誤:

File "C:\Users\user\AppData\Local\Programs\Python\Python38\lib\site-packages\flask_sqlalchemy\__init__.py", line 137, in __init__
    track_modifications = app.config['SQLALCHEMY_TRACK_MODIFICATIONS']
KeyError: 'SQLALCHEMY_TRACK_MODIFICATIONS'

這是我的代碼:

from flask import Flask, request, jsonify
import json
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import Column, Integer, String
from sqlalchemy.schema import FetchedValue

app = Flask(__name__)

db = SQLAlchemy()

class User(db.Model):
    __tablename__ = 'users'
    __table_args__ = {'schema': 'tvtg'}

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(350), nullable=False)
    email = db.Column(db.String(350), nullable=False)
    password = db.Column(db.String(350), nullable=False)
    


@app.route('/')
def hello_world():
    print('HERE')
    peter = User.query.filter_by(name='peter').first()
    print(peter)
    return 'hello_world'


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

誰能幫我找出我做錯了什么? 錯誤的回溯對我沒有多大幫助

實際上,看起來在使用flask-sqlalchemy時,您必須為SQLALCHEMY_TRACK_MODIFICATIONS設置一個值 - 即使文檔另有說明(默認為None )。

是否要將其設置為TrueFalse取決於您的用例,請參閱文檔

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

或者看看這個很棒的教程

https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-iv-database

未發布的Flask-Sqlalchemy版本 3 設置了新的默認值False

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM