weixin_39890652 2020-11-30 11:37
浏览 0

Alembic can't connect to MariaDB

Migrated issue, originally created by houmie ()

I'm using Python 3.6.3 and https://github.com/PyMySQL/mysqlclient-python.

I can connect to the database from the app. But I can't seem to be able to run Alembic upgrade head.

Despite the correct url, it seems that MariaDB can't extract the database from the URL after all.


SQLALCHEMY_DATABASE_URI = 'mysql://root:admin/tegant_db'

I even tried this:


SQLALCHEMY_DATABASE_URI = 'mysql+mysqldb://root:admin/tegant_db'

Below is the stack trace.


/Users/houmie/.pyenv/versions/t-env/bin/python3.6 /Users/houmie/.pyenv/versions/t-env/bin/alembic upgrade head
INFO  [alembic.runtime.migration] Context impl MySQLImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
Traceback (most recent call last):
  File "/Users/houmie/.pyenv/versions/t-env/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1182, in _execute_context
    context)
  File "/Users/houmie/.pyenv/versions/t-env/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 470, in do_execute
    cursor.execute(statement, parameters)
  File "/Users/houmie/.pyenv/versions/t-env/lib/python3.6/site-packages/MySQLdb/cursors.py", line 250, in execute
    self.errorhandler(self, exc, value)
  File "/Users/houmie/.pyenv/versions/t-env/lib/python3.6/site-packages/MySQLdb/connections.py", line 50, in defaulterrorhandler
    raise errorvalue
  File "/Users/houmie/.pyenv/versions/t-env/lib/python3.6/site-packages/MySQLdb/cursors.py", line 247, in execute
    res = self._query(query)
  File "/Users/houmie/.pyenv/versions/t-env/lib/python3.6/site-packages/MySQLdb/cursors.py", line 411, in _query
    rowcount = self._do_query(q)
  File "/Users/houmie/.pyenv/versions/t-env/lib/python3.6/site-packages/MySQLdb/cursors.py", line 374, in _do_query
    db.query(q)
  File "/Users/houmie/.pyenv/versions/t-env/lib/python3.6/site-packages/MySQLdb/connections.py", line 277, in query
    _mysql.connection.query(self, query)
_mysql_exceptions.OperationalError: (1046, 'No database selected')

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/houmie/.pyenv/versions/t-env/bin/alembic", line 11, in <module>
    load_entry_point('alembic==0.9.6', 'console_scripts', 'alembic')()
  File "/Users/houmie/.pyenv/versions/t-env/lib/python3.6/site-packages/alembic/config.py", line 479, in main
    CommandLine(prog=prog).main(argv=argv)
  File "/Users/houmie/.pyenv/versions/t-env/lib/python3.6/site-packages/alembic/config.py", line 473, in main
    self.run_cmd(cfg, options)
  File "/Users/houmie/.pyenv/versions/t-env/lib/python3.6/site-packages/alembic/config.py", line 456, in run_cmd
    **dict((k, getattr(options, k, None)) for k in kwarg)
  File "/Users/houmie/.pyenv/versions/t-env/lib/python3.6/site-packages/alembic/command.py", line 254, in upgrade
    script.run_env()
  File "/Users/houmie/.pyenv/versions/t-env/lib/python3.6/site-packages/alembic/script/base.py", line 425, in run_env
    util.load_python_file(self.dir, 'env.py')
  File "/Users/houmie/.pyenv/versions/t-env/lib/python3.6/site-packages/alembic/util/pyfiles.py", line 81, in load_python_file
    module = load_module_py(module_id, path)
  File "/Users/houmie/.pyenv/versions/t-env/lib/python3.6/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 75, in <module>
    run_migrations_online()
  File "migrations/env.py", line 70, in run_migrations_online
    context.run_migrations()
  File "<string>", line 8, in run_migrations
  File "/Users/houmie/.pyenv/versions/t-env/lib/python3.6/site-packages/alembic/runtime/environment.py", line 836, in run_migrations
    self.get_context().run_migrations(**kw)
  File "/Users/houmie/.pyenv/versions/t-env/lib/python3.6/site-packages/alembic/runtime/migration.py", line 312, in run_migrations
    heads = self.get_current_heads()
  File "/Users/houmie/.pyenv/versions/t-env/lib/python3.6/site-packages/alembic/runtime/migration.py", line 258, in get_current_heads
    if not self._has_version_table():
  File "/Users/houmie/.pyenv/versions/t-env/lib/python3.6/site-packages/alembic/runtime/migration.py", line 269, in _has_version_table
    self.connection, self.version_table, self.version_table_schema)
  File "/Users/houmie/.pyenv/versions/t-env/lib/python3.6/site-packages/sqlalchemy/dialects/mysql/base.py", line 1703, in has_table
    skip_user_error_events=True).execute(st)
  File "/Users/houmie/.pyenv/versions/t-env/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 939, in execute
    return self._execute_text(object, multiparams, params)
  File "/Users/houmie/.pyenv/versions/t-env/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1097, in _execute_text
    statement, parameters
  File "/Users/houmie/.pyenv/versions/t-env/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1189, in _execute_context
    context)
  File "/Users/houmie/.pyenv/versions/t-env/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1402, in _handle_dbapi_exception
    exc_info
  File "/Users/houmie/.pyenv/versions/t-env/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 203, in raise_from_cause
    reraise(type(exception), exception, tb=exc_tb, cause=cause)
  File "/Users/houmie/.pyenv/versions/t-env/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 186, in reraise
    raise value.with_traceback(tb)
  File "/Users/houmie/.pyenv/versions/t-env/lib/python3.6/site-packages/sqlalchemy/engine/base.py", line 1182, in _execute_context
    context)
  File "/Users/houmie/.pyenv/versions/t-env/lib/python3.6/site-packages/sqlalchemy/engine/default.py", line 470, in do_execute
    cursor.execute(statement, parameters)
  File "/Users/houmie/.pyenv/versions/t-env/lib/python3.6/site-packages/MySQLdb/cursors.py", line 250, in execute
    self.errorhandler(self, exc, value)
  File "/Users/houmie/.pyenv/versions/t-env/lib/python3.6/site-packages/MySQLdb/connections.py", line 50, in defaulterrorhandler
    raise errorvalue
  File "/Users/houmie/.pyenv/versions/t-env/lib/python3.6/site-packages/MySQLdb/cursors.py", line 247, in execute
    res = self._query(query)
  File "/Users/houmie/.pyenv/versions/t-env/lib/python3.6/site-packages/MySQLdb/cursors.py", line 411, in _query
    rowcount = self._do_query(q)
  File "/Users/houmie/.pyenv/versions/t-env/lib/python3.6/site-packages/MySQLdb/cursors.py", line 374, in _do_query
    db.query(q)
  File "/Users/houmie/.pyenv/versions/t-env/lib/python3.6/site-packages/MySQLdb/connections.py", line 277, in query
    _mysql.connection.query(self, query)
sqlalchemy.exc.OperationalError: (_mysql_exceptions.OperationalError) (1046, 'No database selected') [SQL: 'DESCRIBE `alembic_version`']

Process finished with exit code 1
</string></module></frozen></frozen></module>

该提问来源于开源项目:sqlalchemy/alembic

  • 写回答

5条回答 默认 最新

  • weixin_39890652 2020-11-30 11:37
    关注

    houmie () wrote:

    I'm sorry, despite hours of trying, I found the reason right after posting this. Apologies.

    For those running into the same isue:

    Edit env.py under Alembic folder

    
    def run_migrations_online():
        conf = config.get_section(config.config_ini_section)
        conf['sqlalchemy.url'] = t_config.SQLALCHEMY_DATABASE_URI
        connectable = engine_from_config(
            conf,
            prefix='sqlalchemy.',
            poolclass=pool.NullPool)
    

    Make sure connectable is passed in the conf and not config.get_section(config.config_ini_section)

    评论

报告相同问题?