Automap reflects tables in postgres schema with sqlalchemy

I am following the sqlalchemy documentation for displaying database tables with automap: http://docs.sqlalchemy.org/en/latest/orm/extensions/automap.html#generating-mappings-from-an-existing-metadata .

When I don't specify the schema and Postgres uses the publicdefault schema , this works as expected, and I find the names of my tables:

>>> m = MetaData()
>>> b = automap_base(bind=engine, metadata=m)
>>> b.prepare(engine, reflect=True)
>>> b.classes.keys()
['ads', 'spatial_ref_sys', 'income']

But when I set the explicit schema, I no longer have access to the tables in Base.classes.

>>> m = MetaData(schema='geography')
>>> b = automap_base(bind=engine, metadata=m)
>>> b.prepare(engine, reflect=True)
>>> b.classes.keys()
[]

MetaData correctly reflected:

>>> b.metadata.tables
immutabledict({geography.usa_cbsa_centroids': Table('usa_cbsa_centroids', MetaData(bind=Engine(postgresql://asteroids:***@localhost:5432/asteroids)), Column('GEOID', VARCHAR(length=5), table=<u
sa_cbsa_centroids>, nullable=False), ...})

Note that tables and columns are known only at run time.

+4
source share

Source: https://habr.com/ru/post/1584977/


All Articles