I am using SQLAlchemy version 2.0.19 (latest public release).
I am trying to map existing tables as documented in https://docs.sqlalchemy.org/en/20/orm/extensions/automap.html#basic-use
I created a SQLite table called user
:
sqlite3 /tmp/mydatabase.db
SQLite version 3.39.5 2022-10-14 20:58:05
Enter ".help" for usage hints.
sqlite>
sqlite> .schema user
CREATE TABLE user (id int, a int);
sqlite>
I am trying to automap this table using SQLAlchemy but the table is not available in Base.classes
as documented:
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy import create_engineBase = automap_base()# engine
engine = create_engine("sqlite:////tmp/mydatabase.db")# reflect the tables
Base.prepare(autoload_with=engine)print(Base.classes.keys())# mapped classes are now created with names by default
# matching that of the table name.
User = Base.classes.user
print(Base.classes.keys())
returns []
.
User = Base.classes.user
shows the table not mapped:
Traceback (most recent call last):File "/***/venv/lib/python3.10/site-packages/sqlalchemy/util/_collections.py", line 186, in __getattr__return self._data[key]
KeyError: 'user'During handling of the above exception, another exception occurred:Traceback (most recent call last):File "/***/test.py", line 17, in <module>User = Base.classes.userFile "/***/venv/lib/python3.10/site-packages/sqlalchemy/util/_collections.py", line 188, in __getattr__raise AttributeError(key)
AttributeError: user
I am a little confused because the example is pretty straightforward and nonetheless it does not appear to work.