问题描述
AttributeError:sqlalchemy对象没有属性"Models"有时ModuleNotFoundError:不会发生名为模型"的模块错误.我已经安装了所有要求,例如pip install sqlalchemy,flask-sqlalchemy,psycopy2-binary.
AttributeError: sqlalchemy object has no attribute "Models"and some times ModuleNotFoundError: No module named 'models' error is occurring. I already installed all requirements like pip install sqlalchemy, flask-sqlalchemy, psycopy2-binary.
import os
from flask import Flask, render_template, request
from models import *
app = Flask(__name__)
app.config["SQLALchemy_DATABASE_URI"] = os.getenv("DATABASE_URL")
app.config["SQLALchemy_TRACK_MODIFICATIONS"] = False
db.init_app(app)
def main():
db.create_all()
if __name__ == "__main__":
with app.app_context():
main()
回溯(最近通话最近):文件"create.py",第5行,在从模型导入*AttributeError:sqlalchemy对象没有属性模型"
Traceback (most recent call last): File "create.py", line 5, in from models import *AttributeError: sqlalchemy object has no attribute "Models"
推荐答案
在您的models.py文件中确保您的数据库对象是
Make shure in your models.py in your db object is
db = SQLAlchemy()
db = SQLAlchemy()
不是db = SQLAlchemy,因为SQLAlchemy是一个类
NOT db = SQLAlchemy , because SQLAlchemy is a class
这篇关于AttributeError:"SQLAlchemy"对象没有属性"Models"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!