eralchemy
eralchemy copied to clipboard
KeyError: '_data'
With
- Python 3.9.2
- sqlalchemy 1.4.0
I get this error:
Traceback (most recent call last):
File "/home/julian/src/bruce-leads/.venv/lib/python3.9/site-packages/sqlalchemy/sql/base.py", line 1104, in __getattr__
return self._index[key]
KeyError: '_data'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/julian/src/bruce-leads/.venv/bin/eralchemy", line 8, in <module>
sys.exit(cli())
File "/home/julian/src/bruce-leads/.venv/lib/python3.9/site-packages/eralchemy/main.py", line 31, in cli
render_er(
File "/home/julian/src/bruce-leads/.venv/lib/python3.9/site-packages/eralchemy/main.py", line 231, in render_er
tables, relationships = all_to_intermediary(input, schema=schema)
File "/home/julian/src/bruce-leads/.venv/lib/python3.9/site-packages/eralchemy/main.py", line 147, in all_to_intermediary
return database_to_intermediary(filename_or_input, schema=schema)
File "/home/julian/src/bruce-leads/.venv/lib/python3.9/site-packages/eralchemy/sqla.py", line 82, in database_to_intermediary
return declarative_to_intermediary(Base)
File "/home/julian/src/bruce-leads/.venv/lib/python3.9/site-packages/eralchemy/sqla.py", line 61, in declarative_to_intermediary
return metadata_to_intermediary(base.metadata)
File "/home/julian/src/bruce-leads/.venv/lib/python3.9/site-packages/eralchemy/sqla.py", line 54, in metadata_to_intermediary
tables = [table_to_intermediary(table) for table in metadata.tables.values()]
File "/home/julian/src/bruce-leads/.venv/lib/python3.9/site-packages/eralchemy/sqla.py", line 54, in <listcomp>
tables = [table_to_intermediary(table) for table in metadata.tables.values()]
File "/home/julian/src/bruce-leads/.venv/lib/python3.9/site-packages/eralchemy/sqla.py", line 49, in table_to_intermediary
return Table(name=table.fullname, columns=[column_to_intermediary(col) for col in table.c._data.values()])
File "/home/julian/src/bruce-leads/.venv/lib/python3.9/site-packages/sqlalchemy/sql/base.py", line 1106, in __getattr__
util.raise_(AttributeError(key), replace_context=err)
File "/home/julian/src/bruce-leads/.venv/lib/python3.9/site-packages/sqlalchemy/util/compat.py", line 180, in raise_
raise exception
AttributeError: _data
I have investigated to fix it for sqlachemy but don't know about backwards compat.
https://github.com/Alexis-benoist/eralchemy/blob/d6fcdc67d6d413bb174bf008fd360044e1dff5a7/eralchemy/sqla.py#L50
needs to be replaced with:
columns=[column_to_intermediary(col) for col in table.c._colset]
I can create a tested PR if you are willing to merge it.
Worked for me too
up
also happens in python 3.8 and ERAlchemy==1.2.10/ SQLAlchemy==1.4.3
Hitting same issue, same versions mentioned.
Same issue for me. The change fixed the problem for me.
A possible fix that should allow for backwards compatibility is to use the builtin getattr(...)
function in the list comprehension.
something like this is working for me:
def table_to_intermediary(table):
"""Transform an SQLAlchemy Table object to it's intermediary representation. """
return Table(
name=table.fullname,
# columns=[column_to_intermediary(col) for col in table.c._data.values()]
# columns=[column_to_intermediary(col) for col in (table.c._data.values() if hasattr(table.c,"_data") else table.c._colset)]
columns=[column_to_intermediary(col) for col in getattr(table.c,"_colset",getattr(table.c,"_data",{}).values())]
)
EDIT: forgot the python syntax tag, and while I was here I included commented lines with the original implementation and a slightly more verbose alternative implementation.
Another workaround, if you don't need 1.4 (release notes on docs.sqlalchemy.org), pinning sqlalchemy<1.4
would fix this. To test: pip install 'sqlalchemy<1.4'
Another workaround, if you don't need 1.4 (release notes on docs.sqlalchemy.org), pinning
sqlalchemy<1.4
would fix this. To test:pip install 'sqlalchemy<1.4'
This worked for me. Thanks!
columns=[column_to_intermediary(col) for col in table.c._colset] is still needful with
- Python 3.10.2
- SQLAlchemy 1.4.31
I added a fork of eralchemy in this repo which contains fixes for the current SQLAlchemy version: #94 https://github.com/maurerle/eralchemy2
Fix works for me!
i am getting this same issue python 3.10.6 sqlalchemy 2.0.16
Hi @tarunsengar1987 , you can use "pip install eralchemy2" instead which has fixes for issues like this: https://github.com/maurerle/eralchemy2