39

I'm learning flask web microframework and after initialization of my database I run flask db init I run flask db migrate, to migrate my models classes to the database and i got an error. I work on Windows 10, the database is MySQL, and extensions install are flask-migrate, flask-sqlalchemy, flask-login.

(env) λ flask db migrate
Traceback (most recent call last):
  File "c:\python36\Lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "c:\python36\Lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "C:\Users\aka\Dev\dream-team\env\Scripts\flask.exe\__main__.py", line 9, in <module>
  File "c:\users\aka\dev\dream-team\env\lib\site-packages\flask\cli.py", line 513, in main
    cli.main(args=args, prog_name=name)
  File "c:\users\aka\dev\dream-team\env\lib\site-packages\flask\cli.py", line 380, in main
    return AppGroup.main(self, *args, **kwargs)
  File "c:\users\aka\dev\dream-team\env\lib\site-packages\click\core.py", line 697, in main
    rv = self.invoke(ctx)
  File "c:\users\aka\dev\dream-team\env\lib\site-packages\click\core.py", line 1066, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "c:\users\aka\dev\dream-team\env\lib\site-packages\click\core.py", line 1066, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "c:\users\aka\dev\dream-team\env\lib\site-packages\click\core.py", line 895, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "c:\users\aka\dev\dream-team\env\lib\site-packages\click\core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "c:\users\aka\dev\dream-team\env\lib\site-packages\click\decorators.py", line 17, in new_func
    return f(get_current_context(), *args, **kwargs)
  File "c:\users\aka\dev\dream-team\env\lib\site-packages\flask\cli.py", line 257, in decorator
    return __ctx.invoke(f, *args, **kwargs)
  File "c:\users\aka\dev\dream-team\env\lib\site-packages\click\core.py", line 535, in invoke
    return callback(*args, **kwargs)
  File "c:\users\aka\dev\dream-team\env\lib\site-packages\flask_migrate\cli.py", line 90, in migrate
    rev_id, x_arg)
  File "c:\users\aka\dev\dream-team\env\lib\site-packages\flask_migrate\__init__.py", line 197, in migrate
    version_path=version_path, rev_id=rev_id)
  File "c:\users\aka\dev\dream-team\env\lib\site-packages\alembic\command.py", line 176, in revision
    script_directory.run_env()
  File "c:\users\aka\dev\dream-team\env\lib\site-packages\alembic\script\base.py", line 427, in run_env
    util.load_python_file(self.dir, 'env.py')
  File "c:\users\aka\dev\dream-team\env\lib\site-packages\alembic\util\pyfiles.py", line 81, in load_python_file
    module = load_module_py(module_id, path)
  File "c:\users\aka\dev\dream-team\env\lib\site-packages\alembic\util\compat.py", line 83, in load_module_py
    spec.loader.exec_module(module)
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "migrations\env.py", line 87, in <module>
    run_migrations_online()
  File "migrations\env.py", line 70, in run_migrations_online
    poolclass=pool.NullPool)
  File "c:\users\aka\dev\dream-team\env\lib\site-packages\sqlalchemy\engine\__init__.py", line 465, in engine_from_config
    return create_engine(url, **options)
  File "c:\users\aka\dev\dream-team\env\lib\site-packages\sqlalchemy\engine\__init__.py", line 424, in create_engine
    return strategy.create(*args, **kwargs)
  File "c:\users\aka\dev\dream-team\env\lib\site-packages\sqlalchemy\engine\strategies.py", line 50, in create
    u = url.make_url(name_or_url)
  File "c:\users\aka\dev\dream-team\env\lib\site-packages\sqlalchemy\engine\url.py", line 211, in make_url
    return _parse_rfc1738_args(name_or_url)
  File "c:\users\aka\dev\dream-team\env\lib\site-packages\sqlalchemy\engine\url.py", line 270, in _parse_rfc1738_args
    "Could not parse rfc1738 URL from string '%s'" % name)
sqlalchemy.exc.ArgumentError: Could not parse rfc1738 URL from string 'mysql/dt_admin:dt2016@localhost/dreamteam_db'
2
  • might be helpful to post your model definitions. I assume the problem is in there! Commented Apr 12, 2018 at 2:42
  • this is my model Commented Apr 12, 2018 at 10:49

6 Answers 6

21

You are not using a valid URL in the connection string.

Review the documentation on how the MySQL connection URLs need to be structured: http://docs.sqlalchemy.org/en/latest/dialects/mysql.html.

Depending on the MySQL driver that you use the connection URL is different. For example, if you use pymysql, your URL should be:

mysql+pymysql://<username>:<password>@<host>/<dbname>[?<options>]
Sign up to request clarification or add additional context in comments.

5 Comments

i'd forget the port number to enter the port, this is the URL connection string: it work now, thank you
it is important NOT to make spaces in the ' '. SQLALCHEMY_DATABASE_URI = 'mysql+mysqlconnector://<username>:<password>@<host>/<dbname>[?<options>]'
@Miguel I am literally using URL.create from engine module to avoid any errors in writing that URL, yet, I'm getting this error.
@AlexDeft Think about it. There is only two possibilities. Option a) you are passing a bad URL and SQLAlchemy rightfully errors and b) you are passing a correct URL and SQLAlchemy has a bug that makes it report a false error. Which one do you think it is? If you think it is b), then file a bug with SQLAlchemy. If you think it is a) then I have no way to help, because you are not showing your work.
@Miguel I restarted the machine and it worked :/
8

i'd forget the port number to enter the port, this is the URL connection string:

`SQLALCHEMY_DATABASE_URI = 'mysql://dt_admin:dt2016@localhost:3308/dreamteam_db'

it work now, thanks

Comments

6

just had to remove the quotes

I was using like that:

sql_alchemy_conn = 'postgresql://user:password@host:port/db'

And this worked:

sql_alchemy_conn = postgresql://user:password@host:port/db

Comments

3

URL in the connection string is not valid.

you can check the documentation on how the MySQL connection URLs need to be structured here : http://docs.sqlalchemy.org/en/latest/dialects/mysql.html.

Example syntax for postgresql with psycopg2 driver it look like this :-

sql_alchemy_conn = postgresql+psycopg2://ubuntu@localhost:5432/airflow

Comments

2

make sure that there is no space or newline in the URI string

Comments

0

For me I got this error once I was trying to fix this issue

 raise TypeError("option values must be strings")
TypeError: option values must be strings

so I tried to stringify the url like so

config.set_main_option(
    "sqlalchemy.url", f'{os.environ.get("DATABASE_URL")}')

After that I got new error because os.environ.get() does not return the Environment Value in windows without exporting the value, here is the error I got

sqlalchemy.exc.ArgumentError: Could not parse rfc1738 URL from string 'None'

So I should either export the value than get it with os.environ.get() or use a config file and get it like so:

config.set_main_option(
    "sqlalchemy.url", settings.database_url.replace("postgres://", "postgresql+asyncpg://", 1))

A detail about how to get env vars using config file you can find it on this answer os.environ.get() does not return the Environment Value in windows?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.