I'm trying to connect my flask application to my local MySQL database for testing. I've made a flask object and a class to represent an example table to be created after a successful connection.
These are my local env vars for my project:
#.env
LOCAL_MYSQL_URL = mysql://Username:[email protected]:3306/database_name
This is my project_name __init__
file:
#__init__.py
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
import os
ON_HEROKU = 'ON_HEROKU' in os.environ
if ON_HEROKU:
DB_URL = os.environ.get('CLEARDB_DATABASE_URL')
else:
DB_URL = os.environ.get('LOCAL_MYSQL_URL')
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = DB_URL
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY')
db = SQLAlchemy(app)
db.create_all()
db.session.commit()
However I get the following error after running the app:
if sa_url.drivername.startswith('mysql'):
AttributeError: 'NoneType' object has no attribute 'drivername'
I am trying to make this work following tutorials and answers contributed here on stackoverlow to similar questions, but none of them helped me out so far.
What am I missing here? What's wrong with my mysql driver name?
KeyError
if you change this line:DB_URL = os.environ.get('LOCAL_MYSQL_URL')
toos.environ['LOCAL_MYSQL_URL']
? – SuperShootraise KeyError(key) from None KeyError: 'MYSQL_URL'
– MattSom'NoneType' object has no attribute 'drivername'
means thatsa_url
isNone
, there's nothing wrong with the driver name, just that the url you are passing to flask-sqlalchemy isNone
andNone.drivername
doesn't exist. – SuperShootpip install pymysql
– cinch.get()
. I'm not sure where to go from here. – SuperShoot