【问题标题】:Reorder composite primary key in sqlalchemy在 sqlalchemy 中重新排序复合主键
【发布时间】:2016-02-11 10:53:03
【问题描述】:

我想在定义我的 sqlalchemy 表时使用继承。

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)

现在我在other_table 上有一个复合主键,即(inserted, blah)。但是,为了查询的效率(假设我正在寻找最新的等等),如果时间戳在整数 (blah, inserted) 之后会更好。有什么奇特的方法可以重新排序主键吗?

【问题讨论】:

    标签: python sqlalchemy


    【解决方案1】:

    是的,使用PrimaryKeyConstraint

    class OtherTable(DeclarativeBase, DatetimeBase):
        __tablename__ = 'other_table'
    
        blah = Column(Integer, primary_key=True)
        other_column = Column(Text)
    
        __table_args__ = (PrimaryKeyConstraint("blah", "inserted"),)
    

    【讨论】:

    • 我已经看到了 PrimarKeyConstraint 的存在,但我不知道如何处理它。我不知道您可以以这种方式使用 table_args。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-14
    • 1970-01-01
    • 1970-01-01
    • 2021-04-24
    • 2016-01-20
    相关资源
    最近更新 更多