Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why does my flask app give this response "class User(db.model): AttributeError: 'SQLAlchemy' object has no attribute 'model'"?

Check the following code I have been working. I am having the problem that my SQLAlchemy is missing model:

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
like image 715
Ezrqn Kemboi Avatar asked Dec 14 '22 18:12

Ezrqn Kemboi


1 Answers

Your class definition should have model starting with capital letter like this:

class User(db.Model):
   # ...
like image 73
plaes Avatar answered Dec 18 '22 00:12

plaes