postgresql - Automap reflect tables within a postgres schema with sqlalchemy -


i'm following sqlalchemy documentation reflecting database tables using automap: http://docs.sqlalchemy.org/en/latest/orm/extensions/automap.html#generating-mappings-from-an-existing-metadata.

when don't specific schema, , postgres uses default public schema, works expected, , find names of 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 specific explicit schema, don't have access tables in base.classes anymore.

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

the metadata reflected correctly though:

>>> 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 tables , columns known @ runtime.

the answer database tables in sqlalchemy require primary key, , table didn't have one. there additional information on page: http://docs.sqlalchemy.org/en/latest/faq/ormconfiguration.html#how-do-i-map-a-table-that-has-no-primary-key.

the sqlalchemy orm, in order map particular table, needs there @ least 1 column denoted primary key column; multiple-column, i.e. composite, primary keys of course entirely feasible well. these columns not need known database primary key columns, though it’s idea are. it’s necessary columns behave primary key does, e.g. unique , not nullable identifier row.

thanks michael bayer answering on sqlalchemy mailing list: https://groups.google.com/forum/#!topic/sqlalchemy/8f2tpkpr4be