Flask-Migrate: "option values must be strings" error on db migrate

I run

$ python run.py db init

And that runs without issue. Then,

$ python run.py db migrate -m 'First migration'
Traceback (most recent call last):
  File "run.py", line 3, in <module>
    myapp.manager.run()
  File "/Users/bfh/.virtualenvs/myapp/lib/python2.7/site-packages/flask_script/__init__.py", line 412, in run
    result = self.handle(sys.argv[0], sys.argv[1:])
  File "/Users/bfh/.virtualenvs/myapp/lib/python2.7/site-packages/flask_script/__init__.py", line 383, in handle
    res = handle(*args, **config)
  File "/Users/bfh/.virtualenvs/myapp/lib/python2.7/site-packages/flask_script/commands.py", line 216, in __call__
    return self.run(*args, **kwargs)
  File "/Users/bfh/.virtualenvs/myapp/lib/python2.7/site-packages/flask_migrate/__init__.py", line 136, in migrate
    version_path=version_path, rev_id=rev_id)
  File "/Users/bfh/.virtualenvs/myapp/lib/python2.7/site-packages/alembic/command.py", line 113, in revision
    script.run_env()
  File "/Users/bfh/.virtualenvs/myapp/lib/python2.7/site-packages/alembic/script.py", line 390, in run_env
    util.load_python_file(self.dir, 'env.py')
  File "/Users/bfh/.virtualenvs/myapp/lib/python2.7/site-packages/alembic/util.py", line 243, in load_python_file
    module = load_module_py(module_id, path)
  File "/Users/bfh/.virtualenvs/myapp/lib/python2.7/site-packages/alembic/compat.py", line 79, in load_module_py
    mod = imp.load_source(module_id, path, fp)
  File "migrations/env.py", line 19, in <module>
    config.set_main_option('sqlalchemy.url', current_app.config.get('SQLALCHEMY_DATABASE_URI'))
  File "/Users/bfh/.virtualenvs/myapp/lib/python2.7/site-packages/alembic/config.py", line 198, in set_main_option
    self.file_config.set(self.config_ini_section, name, value)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ConfigParser.py", line 743, in set
    raise TypeError("option values must be strings")
TypeError: <flask_script.commands.Command object at 0x107aba950>: option values must be strings

Any advice for a fella?

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 26 (7 by maintainers)

Most upvoted comments

Do you have SQLALCHEMY_DATABASE_URI set in your project’s configuration?

@miguelgrinberg Thanks that reply saved me big time

@sim0nsl Do this instead:

class Config(object):
    SECRET_KEY = os.environ.get('SECRET_KEY') or 'you-will-never-guess'
    SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL')
    SQLALCHEMY_TRACK_MODIFICATIONS = False

And then set the following in your environment before running the flask db migrate command:

export DATABASE_URL=mysql://root:mypassword@localhost/mydatabase

@haroonhassan well, the stack trace does not lie, if I’m not mistaken line 22 in migrations/env.py is the line where Alembic tries to get the database URL. You may want to add a print statement above that line to confirm that the configuration is actually available in current_app.config.