SQLALchemy 增加了显着的过载。SQLAlchemy 对象没有属性“dateTime”

1 python sqlalchemy flask-sqlalchemy

我的代码有问题,尝试使用 python 在 Flask 中设置 SQLAlchemy 数据库。代码:

谢谢您的帮助。

尝试重新安装 SQLAlchemy。

我使用的是公司笔记本电脑,应该不会有问题吧?

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

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
db = SQLAlchemy(app)

class Todo(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    content = db.Column(db.String(200), nullable=False)
    date_created = db.Column(db.DateTime, default=datetime

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

@app.route('/', methods=['POST', 'GET'])
def index():
    return render_template('index.html')

if __name__ == "__main__":
    app.run(debug=True)
(env) C:\Users\rodrigs\Documents\PythonFlaskApp>app.py
C:\Users\rodrigs\Documents\PythonFlaskApp\env\lib\site-packages\flask_sqlalchemy\__init__.py:835: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True or False to suppress this warning.
  'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
Traceback (most recent call last):
  File "C:\Users\rodrigs\Documents\PythonFlaskApp\app.py", line 9, in <module>
    class Todo(db.Model):
  File "C:\Users\rodrigs\Documents\PythonFlaskApp\app.py", line 13, in Todo
    date_created = db.Column(db.dateTime, default=datetime.utcnow)
AttributeError: 'SQLAlchemy' object has no attribute 'dateTime'
Run Code Online (Sandbox Code Playgroud)

小智 8

对于警告:

FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True or False to suppress this warning.
'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
Run Code Online (Sandbox Code Playgroud)

将“SQLALCHEMY_TRACK_MODIFICATIONS”设置为 True/False。最好通过添加以下行来设置 False

app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
Run Code Online (Sandbox Code Playgroud)

在 Flask 应用程序实例化之后,即在以下行之后:

app = Flask(__name__)
Run Code Online (Sandbox Code Playgroud)

对于错误:

 date_created = db.Column(db.dateTime, default=datetime.utcnow)
AttributeError: 'SQLAlchemy' object has no attribute 'dateTime'
Run Code Online (Sandbox Code Playgroud)

有一个拼写错误,类型是 db.DateTime。