I’m creating the engine and the Metadata as follows
JavaScript
x
7
1
engine = create_engine('redshift+psycopg2://USER:PASS.region.com:5439/DBNAME')
2
metadata = MetaData(schema='SCHEMA')
3
metadata.reflect(engine, only=['orders', 'packages'])
4
Base = automap_base(metadata=metadata)
5
Base.prepare()
6
print(Base.classes.packages)
7
Yields AttributeError: packages
, and dir(Base.classes)
returns no attributes with that name, neither with the orders
name. Taking the only=['orders', 'packages']
off makes it reflect only a few random tables.
Now, when using the inspector that comes with native SQLAlchemy the table actually works (link to documentation):
JavaScript
1
5
1
insp = reflection.Inspector.from_engine(engine)
2
print(insp.get_table_names(schema='SCHEMA')) #this prints all tables as expected
3
packages_table = Table('packages', metadata)
4
insp.reflecttable(packages_table, None) #this reflects the table as expected
5
Is this a bug, or am I overlooking something here?
Thanks!
Installed package versions:
JavaScript
1
8
1
alembic 1.4.2
2
Flask 1.1.2
3
Flask-Migrate 2.5.3
4
Flask-SQLAlchemy 2.4.4
5
psycopg2 2.8.5
6
SQLAlchemy 1.3.18
7
sqlalchemy-redshift 0.8.1
8
Advertisement
Answer
SQLAlchemy’s automap extension only reflects tables which have defined primary keys.
From the note in the docs:
[…] for a table to be mapped, it must specify a primary key.