Ricky Levi
Ricky Levi

Reputation: 7997

SQLAlchemy doesn't autoload all my columns

I have an issue, where I use the following:

class Docs(Base):       
    __tablename__ = "docs"

    __table__ = Table(__tablename__, Base.metadata, 
        Column("dID", Integer, primary_key=True),
        Column("d_type", Integer, primary_key=True),
        Column("d_category", Integer, primary_key=True),
        autoload_with=get_global_db_engine())

    __table_args__ = (UniqueConstraint("dID", "d_type", "d_category"),)

    # Class Globals
    COLUMNS = __table__.columns.keys()

Problem is, when I loop throughCOLUMNS - it doesn't list all the columns of __table__, it holds only the columns I pre-defined inside the Table ( 3 cols ).

How do I get COLUMNS to return all of them ?

Upvotes: 0

Views: 749

Answers (1)

sebastian
sebastian

Reputation: 9696

You'll have to set extend_existing to extend your defined table with reflected columns.

From the documenation:

Table.extend_existing will also work in conjunction with Table.autoload to run a new reflection operation against the database, even if a Table of the same name is already present in the target MetaData; newly reflected Column objects and other options will be added into the state of the Table, potentially overwriting existing columns and options of the same name.

So,

__table__ = Table(__tablename__, Base.metadata, 
    Column("dID", Integer, primary_key=True),
    Column("d_type", Integer, primary_key=True),
    Column("d_category", Integer, primary_key=True),
    extend_existing=True,
    autoload_with=get_global_db_engine())

should do the trick.

Upvotes: 2

Related Questions