agronholm / sqlacodegen

Automatic model code generator for SQLAlchemy
Other
1.92k stars 248 forks source link

OperationalError #252

Closed gensart-ai closed 1 year ago

gensart-ai commented 1 year ago

Hi,

To the point, i really don't know if this problem occurs in the SQLAlchemy itself or from your library, but it might help newcomers (maybe). Firstly, you can see that when i'm trying to achieve a result with my command :

sqlacodegen mysql+pymysql://root:@localhost/flaskboard

It always returns an error like this :

sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (1049, "Unknown database 'Engine(mysql+pymysql://root:***@localhost:3306/flaskboard)'")
[SQL: SHOW FULL TABLES FROM `Engine(mysql+pymysql://root:***@localhost:3306/flaskboard)`]

I have a flaskboard database in my local mysql. Not sure, i try to change the flaskboard with notyourdb which is the database name that is not included in my local mysql, and it returns :

sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (1049, "Unknown database 'notyourdb'")

Maybe this information might help :

SQLAlchemy version = 2.0.3
PyMySQL version = 1.0.2
sqlacodegen version = 2.3.0
Python version = 3.9.2
MySQL version = MariaDB 10.4.27
OS = Debian 11

Any help/links/forward may be appreciated.

TL;DR The detailed error :

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

Traceback (most recent call last):
  File "/home/gensart/Desktop/PythonSpace/sqlacodegentest/venv/bin/sqlacodegen", line 8, in <module>
    sys.exit(main())
  File "/home/gensart/Desktop/PythonSpace/sqlacodegentest/venv/lib/python3.9/site-packages/sqlacodegen/main.py", line 47, in main
    metadata.reflect(engine, args.schema, not args.noviews, tables)
  File "/home/gensart/Desktop/PythonSpace/sqlacodegentest/venv/lib/python3.9/site-packages/sqlalchemy/sql/schema.py", line 5447, in reflect
    with inspection.inspect(bind)._inspection_context() as insp:
  File "/home/gensart/Desktop/PythonSpace/sqlacodegentest/venv/lib/python3.9/site-packages/sqlalchemy/inspection.py", line 111, in inspect
    ret = reg(subject)
  File "/home/gensart/Desktop/PythonSpace/sqlacodegentest/venv/lib/python3.9/site-packages/sqlalchemy/engine/reflection.py", line 304, in _engine_insp
    return Inspector._construct(Inspector._init_engine, bind)
  File "/home/gensart/Desktop/PythonSpace/sqlacodegentest/venv/lib/python3.9/site-packages/sqlalchemy/engine/reflection.py", line 237, in _construct
    init(self, bind)
  File "/home/gensart/Desktop/PythonSpace/sqlacodegentest/venv/lib/python3.9/site-packages/sqlalchemy/engine/reflection.py", line 248, in _init_engine
    engine.connect().close()
  File "/home/gensart/Desktop/PythonSpace/sqlacodegentest/venv/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 3245, in connect
    return self._connection_cls(self)
  File "/home/gensart/Desktop/PythonSpace/sqlacodegentest/venv/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 147, in __init__
    Connection._handle_dbapi_exception_noconnection(
  File "/home/gensart/Desktop/PythonSpace/sqlacodegentest/venv/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 2410, in _handle_dbapi_exception_noconnection
    raise sqlalchemy_exception.with_traceback(exc_info[2]) from e
  File "/home/gensart/Desktop/PythonSpace/sqlacodegentest/venv/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 145, in __init__
    self._dbapi_connection = engine.raw_connection()
  File "/home/gensart/Desktop/PythonSpace/sqlacodegentest/venv/lib/python3.9/site-packages/sqlalchemy/engine/base.py", line 3269, in raw_connection
    return self.pool.connect()
  File "/home/gensart/Desktop/PythonSpace/sqlacodegentest/venv/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 455, in connect
    return _ConnectionFairy._checkout(self)
  File "/home/gensart/Desktop/PythonSpace/sqlacodegentest/venv/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 1270, in _checkout
    fairy = _ConnectionRecord.checkout(pool)
  File "/home/gensart/Desktop/PythonSpace/sqlacodegentest/venv/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 719, in checkout
    rec = pool._do_get()
  File "/home/gensart/Desktop/PythonSpace/sqlacodegentest/venv/lib/python3.9/site-packages/sqlalchemy/pool/impl.py", line 169, in _do_get
    self._dec_overflow()
  File "/home/gensart/Desktop/PythonSpace/sqlacodegentest/venv/lib/python3.9/site-packages/sqlalchemy/util/langhelpers.py", line 147, in __exit__
    raise exc_value.with_traceback(exc_tb)
  File "/home/gensart/Desktop/PythonSpace/sqlacodegentest/venv/lib/python3.9/site-packages/sqlalchemy/pool/impl.py", line 166, in _do_get
    return self._create_connection()
  File "/home/gensart/Desktop/PythonSpace/sqlacodegentest/venv/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 396, in _create_connection
    return _ConnectionRecord(self)
  File "/home/gensart/Desktop/PythonSpace/sqlacodegentest/venv/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 681, in __init__
    self.__connect()
  File "/home/gensart/Desktop/PythonSpace/sqlacodegentest/venv/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 906, in __connect
    pool.logger.debug("Error on connect(): %s", e)
  File "/home/gensart/Desktop/PythonSpace/sqlacodegentest/venv/lib/python3.9/site-packages/sqlalchemy/util/langhelpers.py", line 147, in __exit__
    raise exc_value.with_traceback(exc_tb)
  File "/home/gensart/Desktop/PythonSpace/sqlacodegentest/venv/lib/python3.9/site-packages/sqlalchemy/pool/base.py", line 901, in __connect
    self.dbapi_connection = connection = pool._invoke_creator(self)
  File "/home/gensart/Desktop/PythonSpace/sqlacodegentest/venv/lib/python3.9/site-packages/sqlalchemy/engine/create.py", line 640, in connect
    return dialect.connect(*cargs, **cparams)
  File "/home/gensart/Desktop/PythonSpace/sqlacodegentest/venv/lib/python3.9/site-packages/sqlalchemy/engine/default.py", line 580, in connect
    return self.loaded_dbapi.connect(*cargs, **cparams)
  File "/home/gensart/Desktop/PythonSpace/sqlacodegentest/venv/lib/python3.9/site-packages/pymysql/connections.py", line 353, in __init__
    self.connect()
  File "/home/gensart/Desktop/PythonSpace/sqlacodegentest/venv/lib/python3.9/site-packages/pymysql/connections.py", line 633, in connect
    self._request_authentication()
  File "/home/gensart/Desktop/PythonSpace/sqlacodegentest/venv/lib/python3.9/site-packages/pymysql/connections.py", line 907, in _request_authentication
    auth_packet = self._read_packet()
  File "/home/gensart/Desktop/PythonSpace/sqlacodegentest/venv/lib/python3.9/site-packages/pymysql/connections.py", line 725, in _read_packet
    packet.raise_for_error()
  File "/home/gensart/Desktop/PythonSpace/sqlacodegentest/venv/lib/python3.9/site-packages/pymysql/protocol.py", line 221, in raise_for_error
    err.raise_mysql_exception(self._data)
  File "/home/gensart/Desktop/PythonSpace/sqlacodegentest/venv/lib/python3.9/site-packages/pymysql/err.py", line 143, in raise_mysql_exception
niuyunda commented 1 year ago

I encountered the same problem, did you solve it?

agronholm commented 1 year ago

SQLAcodegen has not been tested to work on SQLAlchemy 2.0.3. But regardless, it looks like something is passing the string Engine(mysql+pymysql://root:***@localhost:3306/flaskboard) to create_engine().

agronholm commented 1 year ago

There was a similar reported issue, and the user confirmed that it worked when downgrading to SQLAlchemy 1.4.

agronholm commented 1 year ago

I've restricted SQLAlchemy version to < 2.0 now.