Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: 'tuple' object has no attribute 'drivername' using Flask SqlAlchemy

I'm trying to connect to a MySQL database using Flask-SqlAlchemy, here's my parameter :

SQLALCHEMY_DATABASE_URI = 'mysql://root:[email protected]/database?charset=utf8'

But when I go to the url, I get this error :

AttributeError: 'tuple' object has no attribute 'drivername'

If I change the SQLALCHEMY_DATABASE_URI to sqlite:///db.sqlite, it works correctly.

What am I missing ?

Note: I also tried mysql+mysqldb://, without any luck.

like image 791
Cyril N. Avatar asked Dec 02 '22 18:12

Cyril N.


1 Answers

This error often comes up due to their being an extra comma after the URL string. So, instead of...

SQLALCHEMY_DATABASE_URI = 'mysql://root:[email protected]/database?charset=utf8'

...you have...

SQLALCHEMY_DATABASE_URI = 'mysql://root:[email protected]/database?charset=utf8',

The extra comma turns the SQLALCHEMY_DATABASE_URI into a tuple with the string as the only value. SQLAlchemy will skip trying to parse the resulting tuple, but Flask-SQLAlchemy will still try to use the "parsed" result.

This is fixed by removing the extra comma.

like image 73
Mark Hildreth Avatar answered Dec 10 '22 04:12

Mark Hildreth