使用sqlalchemy的声明性ORM扩展时,多列索引 [英] Multiple columns index when using the declarative ORM extension of sqlalchemy
问题描述
根据文档:
http:// docs .sqlalchemy.org / en / latest / core / constraints.html#indexes
和sqlalchemy.Column类中的注释,我们应该使用类 sqlalchemy.schema.Index
以指定包含多个多个索引的索引。
但是,直接使用Table对象,像这样:
meta = MetaData()
mytable = Table ',meta,
#索引列,索引为ix_mytable_col1
列('col1',Integer,index = True),
# ix_mytable_col2
列('col2',Integer,index = True,unique = True),
列('col3',Integer),
列,Integer),
列('col5',Integer),
列('col6',Integer),
)
index col3,col4
索引('idx_col34',mytable.c.col3,mytable.c.col4)
b $ b
如果我们使用声明式ORM扩展,我们应该如何做呢?
b $ b __tablename__ ='table_A'
id = Column(Integer,,primary_key = True)
a = Column(String(32))
b = Column(String(32))
我想在列a和b上建立索引。
class A(Base):
__tablename__ ='table_A'
id = Column(Integer,primary_key = True)
a = column(String(32),index = True)
b = Column(String(32),index = True)
b $ b
如果你想要一个复合索引,再次表
在这里存在,因为你只是不必声明它,一切都是一样的确保你在声明式Aa包装器上最近的0.6或0.7被解释为在类声明完成后的 Column
):
class A(Base):
__tablename__ ='table_A'
id = Column(Integer,primary_key = True)
a = Column(String(32))
b = Column(String(32))
索引('my_index',Aa,Ab)
pre> 在0.7中,索引
可以在表
参数,其中声明的是通过 __ table_args __
:
(基本):
__tablename__ ='table_A'
id = Column(Integer,primary_key = True)
a = Column(String(32))
b = )
__table_args__ =(索引('my_index',a,b),)
According to the documentation: http://docs.sqlalchemy.org/en/latest/core/constraints.html#indexes
and the comments in the sqlalchemy.Column class, we should use the class sqlalchemy.schema.Index
to specify an index that contain multiple multiple index.
However, the example shows how to do it by directly using the Table object like this:
meta = MetaData()
mytable = Table('mytable', meta,
# an indexed column, with index "ix_mytable_col1"
Column('col1', Integer, index=True),
# a uniquely indexed column with index "ix_mytable_col2"
Column('col2', Integer, index=True, unique=True),
Column('col3', Integer),
Column('col4', Integer),
Column('col5', Integer),
Column('col6', Integer),
)
# place an index on col3, col4
Index('idx_col34', mytable.c.col3, mytable.c.col4)
How should we do it if we use the declarative ORM extension?
class A(Base):
__tablename__ = 'table_A'
id = Column(Integer, , primary_key=True)
a = Column(String(32))
b = Column(String(32))
I would like an index on column "a" and "b".
those are just Column
objects, index=True flag works normally:
class A(Base):
__tablename__ = 'table_A'
id = Column(Integer, primary_key=True)
a = Column(String(32), index=True)
b = Column(String(32), index=True)
if you'd like a composite index, again Table
is present here as usual you just don't have to declare it, everything works the same (make sure you're on recent 0.6 or 0.7 for the declarative A.a wrapper to be interpreted as a Column
after the class declaration is complete):
class A(Base):
__tablename__ = 'table_A'
id = Column(Integer, primary_key=True)
a = Column(String(32))
b = Column(String(32))
Index('my_index', A.a, A.b)
In 0.7 the Index
can be in the Table
arguments too, which with declarative is via __table_args__
:
class A(Base):
__tablename__ = 'table_A'
id = Column(Integer, primary_key=True)
a = Column(String(32))
b = Column(String(32))
__table_args__ = (Index('my_index', "a", "b"), )
这篇关于使用sqlalchemy的声明性ORM扩展时,多列索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!