Forums

Connecting to my Pythonanywhere MYSQL DB from my app

after configuring everything as started in your manuals, setting the environments variables and the rest, i test my app with a sqlite db which worked fine setting the db url from the environment variables. but when i tried to move to mysql on python anywhere, i setup everything as started, but all to no avail, it keep giving me errors:

My Config.py

class Config:
    SECRET_KEY = os.environ.get('SECRET_KEY')

    SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE')
    SQLALCHEMY_POOL_RECYCLE = os.environ.get('SQLALCHEMY_POOL_RECYCLE')
    SQLALCHEMY_TRACK_MODIFICATIONS = os.environ.get('SQLALCHEMY_TRACK_MODIFICATIONS')

My .env

    DATABASE = "mysql+mysqlconnector://{username}:{password}@{hostname}/{databasename}".format(
                                                    username="myusername",
                                                    password="mydbpassword",
                                                    hostname=myname.mysql.pythonanywhere-services.com",
                                                    databasename="mynewdb",
                                                )
  SQLALCHEMY_POOL_RECYCLE = 299
  SQLALCHEMY_TRACK_MODIFICATIONS = False

  SECRET_KEY = 'myscretekey'

Error message it returns any time i try to do db.create_all from the console:

Traceback (most recent call last):
 File "<stdin>", line 1, in <module>
 File "/home/mydiv/.virtualenvs/env/lib/python3.9/site-packages/flask_sqlalchemy/__init__.py", 
 line 1094, in create_all
 self._execute_for_all_tables(app, bind, 'create_all')
 File "/home/mydiv/.virtualenvs/env/lib/python3.9/site-packages/flask_sqlalchemy/__init__.py", 
 line 1086, in _execute_for_all_tables
 op(bind=self.get_engine(app, bind), **extra)
 File "/home/mydiv/.virtualenvs/env/lib/python3.9/site-packages/flask_sqlalchemy/__init__.py", 
 line 1017, in get_engine
 return connector.get_engine()
 File "/home/mydiv/.virtualenvs/env/lib/python3.9/site-packages/flask_sqlalchemy/__init__.py", 
 line 593, in get_engine
 sa_url, options = self.get_options(sa_url, echo)
 File "/home/mydiv/.virtualenvs/env/lib/python3.9/site-packages/flask_sqlalchemy/__init__.py", 
 line 608, in get_options
 sa_url, options = self._sa.apply_driver_hacks(self._app, sa_url, options)
  File "/home/mydiv/.virtualenvs/env/lib/python3.9/site-packages/flask_sqlalchemy/__init__.py", 
  line 933, in apply_driver_hacks
  if sa_url.drivername.startswith('mysql'):
  AttributeError: 'NoneType' object has no attribute 'drivername'

am using flask_sqlalchemy i imported everything which worked fine on sqlite db

.env files can't contain Python code like yours does -- they have to be simple key-value files. So, instead of having this:

DATABASE = "mysql+mysqlconnector://{username}:{password}@{hostname}/{databasename}".format(
                                                username="myusername",
                                                password="mydbpassword",
                                                hostname=myname.mysql.pythonanywhere-services.com",
                                                databasename="mynewdb",
                                            )

...you would have to have something like

DATABASE=mysql+mysqlconnector://something:somethingelse@somehostname/somedbname

...replacing the "someXXX" things appropriately.