Automap not reflecting tables in Postgres schemas

2,200 views
Skip to first unread message

Sam Zhang

unread,
Apr 27, 2015, 4:29:48 PM4/27/15
to sqlal...@googlegroups.com
Hello,

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

When I don't specific a schema, and Postgres uses the default `public` 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 specific an explicit schema, I don't have access to the 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 that the tables and columns are only known at runtime.

Any thoughts?


Thanks,
Sam

Mike Bayer

unread,
Apr 27, 2015, 6:54:13 PM4/27/15
to sqlal...@googlegroups.com


On 4/27/15 4:29 PM, Sam Zhang wrote:
Hello,

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

When I don't specific a schema, and Postgres uses the default `public` 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 specific an explicit schema, I don't have access to the 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 that the tables and columns are only known at runtime.
Here's a demo that works for me.  Does it work for you?    Do all your tables have primary keys defined?


from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
from sqlalchemy import create_engine, MetaData


engine = create_engine("postgresql://scott:tiger@localhost/test", echo=True)
engine.execute("""
    create table if not exists test_schema.user (
        id serial primary key, name varchar(30)
    )
""")
engine.execute("""
    create table if not exists test_schema.address (
        id serial primary key,
        email_address varchar(30),
        user_id integer references test_schema.user(id)
    )
""")

m = MetaData(schema="test_schema")

Base = automap_base(bind=engine, metadata=m)

# reflect the tables
Base.prepare(engine, reflect=True)

assert Base.classes.keys() == ['user', 'address']

User = Base.classes.user
Address = Base.classes.address


session = Session(engine)

session.add(Address(email_address="f...@bar.com", user=User(name="foo")))
session.commit()

u1 = session.query(User).first()
print(u1.address_collection)







Any thoughts?


Thanks,
Sam
--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Sam Zhang

unread,
Apr 28, 2015, 3:02:53 PM4/28/15
to sqlal...@googlegroups.com
Thanks Michael! it was the lack of a primary key. I see references to it now that I know what to look for
- a very interesting explanation: http://docs.sqlalchemy.org/en/latest/faq/ormconfiguration.html#how-do-i-map-a-table-that-has-no-primary-key

It looks like there's no mention of this requirement in the automap documentation page though: http://docs.sqlalchemy.org/en/latest/orm/extensions/automap.html. I'd be happy to add a brief note about it and submit a pull request if you'd like.

Sam

Mike Bayer

unread,
Apr 28, 2015, 3:46:11 PM4/28/15
to sqlal...@googlegroups.com


On 4/28/15 3:02 PM, Sam Zhang wrote:
Thanks Michael! it was the lack of a primary key. I see references to it now that I know what to look for
- a very interesting explanation: http://docs.sqlalchemy.org/en/latest/faq/ormconfiguration.html#how-do-i-map-a-table-that-has-no-primary-key

It looks like there's no mention of this requirement in the automap documentation page though: http://docs.sqlalchemy.org/en/latest/orm/extensions/automap.html. I'd be happy to add a brief note about it and submit a pull request if you'd like.

sure thing!

Sam Zhang

unread,
Apr 28, 2015, 10:08:54 PM4/28/15
to sqlal...@googlegroups.com
Just hit the issue tracker with this, and the two snags I encountered so far doing this. Hopefully it's not because I missed some glaring instructions about how to build the documentation?

Mike Bayer

unread,
Apr 29, 2015, 12:09:08 AM4/29/15
to sqlal...@googlegroups.com


On 4/28/15 10:08 PM, Sam Zhang wrote:
Just hit the issue tracker with this, and the two snags I encountered so far doing this. Hopefully it's not because I missed some glaring instructions about how to build the documentation?
there's no instructions right now.  it's a sphinx build, plus the things that are in requirements.txt.   the themes and plugins are all extremely custom so you can't modify the themes or anything like that.
Reply all
Reply to author
Forward
0 new messages