为什么我的烧瓶应用程序给出了这个响应“class User(db.model):AttributeError:'SQLAlchemy'对象没有属性'model'”?

时间:2018-01-21 14:29:32

标签: python postgresql flask flask-sqlalchemy

检查以下我一直在使用的代码。我遇到的问题是我的SQLAlchemy缺少模型:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy


app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:password@localhost/flask-movie'
db = SQLAlchemy(app)


class User(db.model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)

    def __init__(self, username, email):
        self.username = username
        self.email = email

    def __repr__(self):
        return '<User %r>' % self.username

1 个答案:

答案 0 :(得分:5)

你的班级定义应该以大写字母开头model,如下所示:

class User(db.Model):
   # ...