python - 为什么我的 Flask 应用程序会给出此响应 "class User(db.model): AttributeError: ' SQLAlchemy' 对象没有属性 'model' “?

标签 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

最佳答案

你的类定义应该有 model 像这样以大写字母开头:

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

关于python - 为什么我的 Flask 应用程序会给出此响应 "class User(db.model): AttributeError: ' SQLAlchemy' 对象没有属性 'model' “?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48367887/

相关文章:

python - pandas.nlargest() - 与重复的索引值混淆

python - 加载 psycopg2 模块时出错 - 从 Postgres 应用程序更改为 brew 安装的 postgres

java - Spring Boot/Java 应用程序中的实体错误

python - Flask for Python - 关于系统的架构问题

python - 无法使用类似 gunicorn 的服务运行 Flask 应用程序

python - gensim Word2vec 迁移学习(来自非 gensim 模型)

python - For循环不打印项目

coding-style - 在 Python 中创建常量的约定

sql - 如何在 PostgreSQL 中使用带有 RANDOM() 函数的 SELECT DISTINCT?

python - 在 XAMPP ubuntu 和 mac os x 中安装 a2enmod