passren / PyDynamoDB

PyDynamoDB is a Python DB API 2.0 (PEP 249) client for Amazon DynamoDB. A SQLAlchemy dialect is offered as well. Superset official database driver.
Other
17 stars 3 forks source link

Support for SQLAlchemy v2+ #53

Open mkkatica opened 3 months ago

mkkatica commented 3 months ago

Hello! I was planning on using this library in conjunction with SQLModel, and SQLModel requires SQLAlchemy v2. During some initial work when using 0.6.2 of PyDynamoDB and SQLAlchemy 2.0.31, I found some errors that look to be related to implementing v1 (see below). Do you have plans to support SQLAlchemy v2? Or is this just a simple bug?

Error:

Traceback (most recent call last):
  File "/Users/mkkatica/Documents/Workspace/pydantic-apigateway/pydantic_apigateway/api/api.py", line 52, in p
    session.bulk_save_objects([item])
  File "/Users/mkkatica/.pyenv/versions/pydantic-apigateway/lib/python3.12/site-packages/sqlalchemy/orm/session.py", line 4574, in bulk_save_objects
    self._bulk_save_mappings(
  File "/Users/mkkatica/.pyenv/versions/pydantic-apigateway/lib/python3.12/site-packages/sqlalchemy/orm/session.py", line 4751, in _bulk_save_mappings
    with util.safe_reraise():
  File "/Users/mkkatica/.pyenv/versions/pydantic-apigateway/lib/python3.12/site-packages/sqlalchemy/util/langhelpers.py", line 146, in __exit__
    raise exc_value.with_traceback(exc_tb)
  File "/Users/mkkatica/.pyenv/versions/pydantic-apigateway/lib/python3.12/site-packages/sqlalchemy/orm/session.py", line 4740, in _bulk_save_mappings
    bulk_persistence._bulk_insert(
  File "/Users/mkkatica/.pyenv/versions/pydantic-apigateway/lib/python3.12/site-packages/sqlalchemy/orm/bulk_persistence.py", line 200, in _bulk_insert
    result = persistence._emit_insert_statements(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mkkatica/.pyenv/versions/pydantic-apigateway/lib/python3.12/site-packages/sqlalchemy/orm/persistence.py", line 1048, in _emit_insert_statements
    result = connection.execute(
             ^^^^^^^^^^^^^^^^^^^
  File "/Users/mkkatica/.pyenv/versions/pydantic-apigateway/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1418, in execute
    return meth(
           ^^^^^
  File "/Users/mkkatica/.pyenv/versions/pydantic-apigateway/lib/python3.12/site-packages/sqlalchemy/sql/elements.py", line 515, in _execute_on_connection
    return connection._execute_clauseelement(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mkkatica/.pyenv/versions/pydantic-apigateway/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1632, in _execute_clauseelement
    compiled_sql, extracted_params, cache_hit = elem._compile_w_cache(
                                                ^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mkkatica/.pyenv/versions/pydantic-apigateway/lib/python3.12/site-packages/sqlalchemy/sql/elements.py", line 703, in _compile_w_cache
    compiled_sql = self._compiler(
                   ^^^^^^^^^^^^^^^
  File "/Users/mkkatica/.pyenv/versions/pydantic-apigateway/lib/python3.12/site-packages/sqlalchemy/sql/elements.py", line 316, in _compiler
    return dialect.statement_compiler(dialect, self, **kw)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mkkatica/.pyenv/versions/pydantic-apigateway/lib/python3.12/site-packages/sqlalchemy/sql/compiler.py", line 1429, in __init__
    Compiled.__init__(self, dialect, statement, **kwargs)
  File "/Users/mkkatica/.pyenv/versions/pydantic-apigateway/lib/python3.12/site-packages/sqlalchemy/sql/compiler.py", line 870, in __init__
    self.string = self.process(self.statement, **compile_kwargs)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mkkatica/.pyenv/versions/pydantic-apigateway/lib/python3.12/site-packages/sqlalchemy/sql/compiler.py", line 915, in process
    return obj._compiler_dispatch(self, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/mkkatica/.pyenv/versions/pydantic-apigateway/lib/python3.12/site-packages/sqlalchemy/sql/visitors.py", line 141, in _compiler_dispatch
    return meth(self, **kw)  # type: ignore  # noqa: E501
           ^^^^^^^^^^^^^^^^
  File "/Users/mkkatica/.pyenv/versions/pydantic-apigateway/lib/python3.12/site-packages/pydynamodb/sqlalchemy_dynamodb/pydynamodb.py", line 111, in visit_insert
    crud_params = crud._get_crud_params(self, insert_stmt, compile_state, **kw)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: _get_crud_params() missing 1 required positional argument: 'toplevel'
passren commented 3 months ago

@mkkatica Thank you for raising this out. Yeah, I was planning to add SQLAlchemy v2 support but I need some effort to discover the new codebase of v2. Please be patient and I will post the new version once getting done.

passren commented 1 month ago

@mkkatica it supports sqlalchemy 2.x from 0.7.0 now.