Reputation: 24928
Here's a table definitition using SQLAlchemy declarative_base:
t_measures_voyage_crude = Table(
'measures_voyage_crude', metadata,
Column('tanker', String(255)),
Column('load_point', Text),
Column('load_port', String(255)),
Column('load_country', NullType),
Column('load_region', String(255)),
Column('date_depart', Date),
Column('offtake_point', Text),
Column('offtake_port', String(255)),
Column('offtake_country', NullType),
Column('offtake_region', String(255)),
Column('date_arrive', Date),
Column('grade', Text),
Column('bbls', BigInteger),
Column('row_num', Integer, index=True),
Column('date_num', Integer, index=True),
Column('stat_num', Integer, index=True),
schema='numbergo'
)
It has three indexed fields, but if I have the table object in a variable, how do I list these columns that have indices?
Similarly for a class, say this one:
class MasterBpd(Base):
__tablename__ = 'master_bpd'
__table_args__ = {'schema': 'numbergo'}
date = Column(Date, primary_key=True)
bpd_imp_yr = Column(Integer)
bpd_imp_mo = Column(Integer)
bpd_imp_wk = Column(Integer)
bpd_dom_yr = Column(Integer)
bpd_dom_mo = Column(Integer)
bpd_dom_wk = Column(Integer)
row_num = Column(BigInteger, nullable=False, index=True)
date_num = Column(BigInteger, nullable=False, index=True)
How do I check programmatically which fields have indices, and which is the primary key?
Upvotes: 1
Views: 1372
Reputation: 43
for col in MasterBpd.__table__.columns:
print(col.index) # True if this field has index, None otherwise
print(col.primary_key) # True if is primary key, False otherwise
Upvotes: 2