artdv
artdv

Reputation: 794

Reorder composite primary key in sqlalchemy

I want to use inheritance when defining my sqlalchemy tables.

class DatetimeBase(object):
    inserted = Column(DateTime, primary_key=True)

class OtherTable(DeclarativeBase, DatetimeBase):
    __tablename__ = 'other_table'

    blah = Column(Integer, primary_key=True)
    other_column = Column(Text)

Now I have a composite primary key on other_table, namely (inserted, blah). However, for efficiency of queries (assuming I'm looking for the latest blah, etc.), it would be better if the timestamp came after the integer, (blah, inserted). Is there some fancy way to reorder the primary key?

Upvotes: 1

Views: 162

Answers (1)

univerio
univerio

Reputation: 20528

Yes, using PrimaryKeyConstraint:

class OtherTable(DeclarativeBase, DatetimeBase):
    __tablename__ = 'other_table'

    blah = Column(Integer, primary_key=True)
    other_column = Column(Text)

    __table_args__ = (PrimaryKeyConstraint("blah", "inserted"),)

Upvotes: 3

Related Questions