eralchemy icon indicating copy to clipboard operation
eralchemy copied to clipboard

KeyError: '_data'

Open julian-r opened this issue 3 years ago • 12 comments

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.

julian-r avatar Mar 19 '21 10:03 julian-r

Worked for me too

emilheunecke avatar Mar 23 '21 11:03 emilheunecke

up

also happens in python 3.8 and ERAlchemy==1.2.10/ SQLAlchemy==1.4.3

andrescevp avatar Mar 27 '21 22:03 andrescevp

Hitting same issue, same versions mentioned.

LuisPeregrinaIBM2 avatar Mar 30 '21 22:03 LuisPeregrinaIBM2

Same issue for me. The change fixed the problem for me.

lulou avatar Apr 11 '21 10:04 lulou

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.

RyanCPeters avatar May 25 '21 18:05 RyanCPeters

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'

tony avatar Jun 15 '21 21:06 tony

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!

ISipi avatar Jun 29 '21 15:06 ISipi

columns=[column_to_intermediary(col) for col in table.c._colset] is still needful with

  • Python 3.10.2
  • SQLAlchemy 1.4.31

smitty1eGH avatar Feb 19 '22 18:02 smitty1eGH

I added a fork of eralchemy in this repo which contains fixes for the current SQLAlchemy version: #94 https://github.com/maurerle/eralchemy2

maurerle avatar Jun 26 '22 19:06 maurerle

Fix works for me!

vinimlo avatar Sep 30 '22 00:09 vinimlo

i am getting this same issue python 3.10.6 sqlalchemy 2.0.16

tarunsengar1987 avatar Jun 22 '23 14:06 tarunsengar1987

Hi @tarunsengar1987 , you can use "pip install eralchemy2" instead which has fixes for issues like this: https://github.com/maurerle/eralchemy2

maurerle avatar Jun 22 '23 17:06 maurerle