Reputation: 21
I'm new to Flask and Python and I don't understand why it gives me this error:
Traceback (most recent call last):
File "C:\Users\Lucía\Documents\TFG\herfontsistemas-app\herfontsistemas-back\index.py", line 6, in <module>
db.create_all()
File "C:\Users\Lucía\Documents\TFG\herfontsistemas-app\herfontsistemas-back\env\lib\site-packages\flask_sqlalchemy\extension.py", line 868, in create_all
self._call_for_binds(bind_key, "create_all")
File "C:\Users\Lucía\Documents\TFG\herfontsistemas-app\herfontsistemas-back\env\lib\site-packages\flask_sqlalchemy\extension.py", line 846, in _call_for_binds
raise sa.exc.UnboundExecutionError(message) from None
sqlalchemy.exc.UnboundExecutionError: 'SQLALCHEMY_DATABASE_URI' config is not set. Bind key 'None' is not in 'SQLALCHEMY_BINDS' config.
This is my app.py:
from flask import Flask
from routes.contacts import contacts
from flask_sqlalchemy import SQLAlchemy
app=Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI']='mysql://root:lucia@localhost/herfontsistemasdb'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS']=False
SQLAlchemy(app)
app.register_blueprint(contacts)
This is my index.py:
from app import app
from utils.db import db
#Cuando arranque la aplicacion creará las tablas dee contact.py
with app.app_context():
db.create_all()
if __name__=="__main__":
app.run(debug=True)
This is my contact.py:
from utils.db import db
class Contact(db.Model):
id = db.Column(db.Integer, primary_key=True)
nombreCompleto = db.Column(db.String(100))
email = db.Column(db.String(100))
telefono = db.Column(db.String(100))
def __init__(self,nombreCompleto,email,telefono):
self.nombreCompleto=nombreCompleto
self.email=email
self.telefono=telefono
enter code here
This is my db.py:
from flask_sqlalchemy import SQLAlchemy
db=SQLAlchemy()
Upvotes: 0
Views: 3503
Reputation: 51
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app=Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI']='mysql://root:lucia@localhost/herfontsistemasdb'
<b>
`# Add Secret Key!`
app.config['SECRET_KEY'] = "custom key"
`# create the extension`
db=SQLAlchemy()
`# Initialize The Database`
db.init_app(app)
</b>
`#Make A Model or Schema Like`
class Users(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(20), nullable=False)
email = db.Column(db.String(50), nullable=False, unique=True)
# we add new column with need migrate
address = db.Column(db.String(100))
date_added = db.Column(db.DateTime, default=datetime.utcnow)
# Create aString
def __repr__(self):
return '<Name %r>' % self.name
with app.app_context():
db.create_all()
if __name__=="__main__":
app.run(debug=True)
Upvotes: 0
Reputation: 11
I am assuming you are using Flask-SQLAlchemy version > 3.
You do not have to create your SQLAlchemy(app) in your app.py file. I would give the following a try.
index.py:
from app import app
from utils.db import db
#Cuando arranque la aplicacion creará las tablas dee contact.py
#ADD THIS LINE HERE
db.init_app(app)
with app.app_context():
db.create_all()
if __name__=="__main__":
app.run(debug=True)
app.py:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app=Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI']='mysql://root:lucia@localhost/herfontsistemasdb'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS']=False
See here for more information on how to set up the app in Flask-SQLAlchemy 3.x: https://flask-sqlalchemy.palletsprojects.com/en/3.0.x/quickstart/
Upvotes: 1