0

Im creating a flask app integrated with the flask extension flask_user.

However, I keep getting the following error when i try to run the app:

'SQLALCHEMY_DATABASE_URI' not in app.config and AttributeError: 'SQLAlchemy' object has no attribute 'config'

Here is my code:

app.py

from flask import Flask,render_template
from modules.database import create_db
from config import config
from flask_user import login_required, current_user,UserMixin

app = Flask(__name__)
config_name='development'
app.config.from_object(config[config_name])
db,User,Stripe,Plans,Usage,Tasks=create_db(app)

@app.route('/')
def home():
    return 'Hello'

if __name__ == '__main__':
    app.run()

config.py

class Config:

        USER_APP_NAME='App Test'
        USER_AFTER_REGISTER_ENDPOINT='plans'

class Dev_Config(Config):
        ENV='development'
        DEBUG=True
        SQLALCHEMY_DATABASE_URI='sqlite:///dev_db.db'
        CSRF_ENABLED=True
        SECRET_KEY = '2345frtjislkmnqwers'

class Prod_Config(Config):
        DEBUG=False


config={'development': Dev_Config,
        'production': Prod_Config}

database.py

def create_db(app):

    db = SQLAlchemy(app)

    class User(db.Model, UserMixin):
        id=db.Column(db.Integer,primary_key=True)
        password = db.Column(db.String(255),nullable=False, server_default='')
        email = db.Column(db.String(255),nullable=False,unique=True)
        confirmed_at = db.Column(db.DateTime())
        plan_id=db.Column(db.Integer, db.ForeignKey('plans.id'),default=1)
        active=db.Column(db.Boolean(),nullable=False, server_default='0')

     ...


    class Tasks(db.Model):
        id=db.Column(db.Integer,primary_key=True)
        task=db.Column(db.String(50),nullable=False,unique=True)
        credits=db.Column(db.Integer,nullable=False)

    db.create_all()
    db_adapter=SQLAlchemy(db,User)
    user_manager=UserManager(db_adapter,app)



    return (db,User,Stripe,Plans,Usage,Tasks)

When i run flask, the database is successfully created, however the error message appears.

I dont understand why it is returning the error 'SQLALCHEMY_DATABASE_URI' not in app.config, because clearly in config.py, SQLALCHEMY_DATABASE_URI has been configured. and is present there.

Also what does the error message 'SQLAlchemy' object has no attribute 'config' mean? And why might it be happening?

Can anyone help? Many thanks in advance.

1
  • What is this line doing? db_adapter=SQLAlchemy(db,User). The SQLAlchemy constructor assumes the first positional argument to be an instance of a Flask app, not another SQLAlchemy instance such as you are passing in here. Similarly, the User object you pass in as the second positional argument is being assigned to the use_native_unicode parameter. Commented Nov 2, 2019 at 21:09

2 Answers 2

2

I know this is not the problem you are having (and I believe the reply you got solves your issue), however I got here with the same error but a different issue.

The problem ended up being a simple mistake in the database model's inheritance, however the error sent me in the wrong direction, a bit awkward when I did figure it out! The snippet that gave me the error:

class Layout(db):
    ...

Where db is SQLAchemy(app). This is obviously not right, as it should inherit from db.Model. Fixed:

class Layout(db.Model):
    ...
Sign up to request clarification or add additional context in comments.

Comments

1

I don't know why you are getting that error, but I have done something similar and this worked. In my code I have not made different files called config.py and database.py rather I just used it as app.config and database as db. Try doing it my way and see if that works

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = "sqlite:///My_Project_Database.db"
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db = SQLAlchemy(app)


class user_database(db.Model):
    sno=db.Column(db.Integer,primary_key=True)

class inventory_database(db.Model):
    sno=db.Column(db.Integer,primary_key=True)

class suppliers_database(db.Model):
    sno=db.Column(db.Integer,primary_key=True)
db.create_all()

1 Comment

Add this as a comment to the question rather than as an answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.