I tried to work with CURD
operation using Flask
and SQLAlchemy
.But getting Error
while connecting to database.
Here is the Error
log.
/usr/local/lib/python3.4/dist-packages/flask_sqlalchemy/__init__.py:819: UserWarning: SQLALCHEMY_DATABASE_URI not set. Defaulting to "sqlite:///:memory:".
'SQLALCHEMY_DATABASE_URI not set. Defaulting to '
/usr/local/lib/python3.4/dist-packages/flask_sqlalchemy/__init__.py:839: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning.
'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
here is my code & setup
# database.py
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
sqldb = SQLAlchemy(app)
app.config['SQLALCHEMY_DATABASE_URI'] = "mysql+pymysql://root:root@localhost/myDbName"
# create app
def create_app():
sqlDB = SQLAlchemy(app)
sqlDB.init_app(app)
sqlDB.create_all()
return app
here is models.py
from ..database import create_app
sqldb = create_app()
# Users Model
class Users(sqldb.Model):
__tablename__ = 'users'
id = sqldb.Column(sqldb.Integer, primary_key = True)
db = sqldb.Column(sqldb.String(40))
def __init__(self,email,db):
self.email = email
self.db = db
def __repr__(self,db):
return '<USER %r>' % self.db
here is routes.py
# Import __init__ file
from __init__ import app
import sys
# JSON
from bson import dumps
# login
@app.route('/', methods = ['GET'])
def login():
try:
# import users model
from Model.models import Users,sqldb
sqldb.init_app(app)
sqldb.create_all()
getUser = Users.query.all()
print(getUser)
return 'dsf'
except Exception as e:
print(e)
return "error."
SQLALCHEMY_DATABASE_URI. The database URI that should be used for the connection. Examples: sqlite:////tmp/test.db. mysql://username:password@server/db.
“FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning.” Code Answer's.
Use a bind To use a binded database, you need to specify it with the bind_key attribute. Declare a model using database2 – which is binded to “db2”. Simply, using bind_key you've told Flask-SQLAlchemy to use the db2 (aka database2) database to declare the model.
You probably need to put this line app.config['SQLALCHEMY_DATABASE_URI'] = "mysql..."
before the SQLAlchemy(app)
instanciation.
Another option is to create SQLAlchemy()
without parametters, to configure URI, and finally to tell SQLAlchemy
to link with your app via sqldb.init_app(app)
Note that it is what you've done in your create_app
function, but you never use it ?
Like the answer above, if you use it this way, change
sqldb = SQLAlchemy(app)
to
sqldb = SQLAlchemy()
I got the same error, and it was gone after setting the FLASK_APP environment variable :
export FLASK_APP=run.py
Start the application :
flask run --host=0.0.0.0 --port=5000
This type of error occurs when app.config['SQLALCHEMY_TRACK_MODIFICATIONS']
is not set. You can set it as True
or False
like so:
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
or
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With