gpt4 book ai didi

python - 使用 SQLAlchemy 在 sqlite db 中创建有序索引

转载 作者:行者123 更新时间:2023-12-03 17:51:32 26 4
gpt4 key购买 nike

我正在使用声明性 API 在 SQLAlchemy 中定义一个表。它有一个我想索引的外键。我的问题是:如何定义从 master_ref 创建的索引成为 ASC DESC 索引(无需借助 SQL 手动执行)?

class Item(Base):
id = Column(INTEGER, primary_key=True)
master_ref = Column(INTEGER, ForeignKey('master.id'), nullable=True, index=True)
value = Column(REAL)

看着 documentation在 SqlAlchemy 中,创建索引的另一种方法是:
class Item(Base):
id = Column(INTEGER, primary_key=True)
master_ref = Column(INTEGER, ForeignKey('master.id'))
value = Column(REAL)
Index('ix_name', master_ref)

但我找不到任何关于如何在任何地方定义 ASC 或 DESC 的引用。

最佳答案

您可以使用功能索引,如 documentation 中指定的那样:

Functional Indexes

Index supports SQL and function expressions, as supported by the target backend. To create an index against a column using a descending value, the ColumnElement.desc() modifier may be used:

from sqlalchemy import Index

Index('someindex', mytable.c.somecol.desc())


同样对于升序值,使用 ColumnElement.asc()修饰符。

关于python - 使用 SQLAlchemy 在 sqlite db 中创建有序索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55921584/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com