techwithtim / Flask-Web-App-Tutorial

Code for the note storing flask web app made during a YouTube video.
938 stars 1.03k forks source link

AttributeError: type object 'SQLAlchemy' has no attribute 'Model' #146

Closed drfq closed 9 months ago

drfq commented 9 months ago

Hello Everyone,

I have been following the tutorial up until the part were the DB needs to be created, once I attempt to run the code I get the following errors in my terminal:

PS C:\Users\rober\OneDrive\Desktop\LOGINPROJECT> & C:/Users/rober/AppData/Local/Programs/Python/Python312/python.exe c:/Users/rober/OneDrive/Desktop/LOGINPROJECT/main.py Traceback (most recent call last): File "c:\Users\rober\OneDrive\Desktop\LOGINPROJECT\main.py", line 4, in app = create_app() ^^^^^^^^^^^^ File "c:\Users\rober\OneDrive\Desktop\LOGINPROJECT\pagina__init__.py", line 22, in create_app from pagina import models File "c:\Users\rober\OneDrive\Desktop\LOGINPROJECT\pagina\models.py", line 6, in class Note(db.Model): ^^^^^^^^ AttributeError: type object 'SQLAlchemy' has no attribute 'Model' PS C:\Users\rober\OneDrive\Desktop\LOGINPROJECT>

Picture of my code:

project

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from os import path

db=SQLAlchemy
DB_NAME = "database.db"

def create_app():
    app = Flask(__name__)
    app.config['SECRET_KEY'] = 'MYKEY'
    app.config['SQLALQUEMY_DATABASE'] = F'sqlite:///{DB_NAME}'

    from .auth import auth
    from .views import views

    app.register_blueprint(views, url_prefix='/')
    app.register_blueprint(auth, url_prefix='/')

    from models import  User, Note

    with app.app_context():
        db.create_all()

    return app

def create_database(app):
    if not path.exists('website/'+ DB_NAME):
        db.create_all(app=app)
        print('All working')
drfq commented 9 months ago

Issue Resolved, Fixed typos in:

From app.config['SQLALQUEMY_DATABASE'] = F'sqlite:///{DB_NAME}'

To: app.config['SQLALCHEMY_DATABASE_URI'] = f'sqlite:///{DB_NAME}'