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.
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.