Python SQLAlchemy声明性映射器中的JSONB字段索引

Python SQLAlchemy声明性映射器中的JSONB字段索引,python,json,postgresql,sqlalchemy,Python,Json,Postgresql,Sqlalchemy,我有一个Postgres表的模型,我想在JSONB列中的字段上添加索引。我尝试了几种不同的方法,但未能让alembic自动生成命令,以基于声明性模型创建这些索引 class Content(Base): """ Model for storing content. Content can be of different types. """ __tablename__ = 'content' __table_args__ = ( Inde

我有一个Postgres表的模型,我想在JSONB列中的字段上添加索引。我尝试了几种不同的方法,但未能让alembic自动生成命令,以基于声明性模型创建这些索引

class Content(Base):
    """
    Model for storing content. Content can be of different types. 
    """
    __tablename__ = 'content'
    __table_args__ = (
        Index('ix_content_owner_id', text("owner->>'id'")),
        Index('ix_content_category_id', text("category->>'id'")),
        Index('ix_content_metadata_styles', text("meta->>'styles'")),
        Index('ix_content_metadata_spaces', text("meta->>'s    paces'")),
        Index('ix_content_metadata_colours', text("meta->>'colours'")),
        Index('ix_content_data_store', text("content_data->>'store'")),
        Index('ix_content_data_tags', text("content_data->>'tags'")),
    )

    id            = Column(Integer, primary_key=True, autoincrement=1)
    content_type  = Column(String(64), index=True, nullable=False)
    content_id    = Column(String(128), index=True, nullable=False)
    uploader      = Column(String(128), index=True, nullable=False)
    date_added    = Column(DateTime(timezone=True), index=True, nullable=False,
                       default=utcnow_with_tz)
    date_modified = Column(DateTime(timezone=True), index=True, nullable=False,
                       default=utcnow_with_tz, onupdate=utcnow_with_tz)
    owner         = Column(JSONB, nullable=False)
    category      = Column(JSONB, nullable=False, server_default="'{}'")
    meta          = Column(JSONB, nullable=False, server_default="'{}'")
    content_data  = Column(JSONB, nullable=False)
    relations     = Column(JSONB, nullable=False, server_default="'{}'")
    comments      = Column(JSONB, nullable=False, server_default="'{}'")
    status        = Column(Integer, index=True, nullable=False, server_default="0")
    public        = Column(Integer, index=True, nullable=False, server_default="1")

    __mapper_args__ = {
        'polymorphic_on': content_type,
        'polymorphic_identity': 'content',
    }
我发现这是一个线程,它解释了如何通过coreapi()实现这一点。我已经采用了这种技术来声明
\uuu table\u args\uu
属性中的索引。索引使用文本语法
列->>“字段”


关于如何让alembic生成这些索引的命令,有什么想法吗?

autogen只是一个方便的功能。它需要生成所有内容,PG的函数索引是目前不支持的,因为SQLAlchemy首先没有反映这些内容。当您运行autogenerate时,每个索引的警告都应该很明显。是否无法让alembic为索引使用文本文字?我不理解这个问题。对不起,让我重新措辞。大概SQLAlchemy需要将索引对象转换为SQL字符串表示来生成索引。由于我使用文本(例如:
text(meta->>'styles')
)定义索引,alembic是否可以在不支持PG函数索引的SQL生成的情况下使用文本?PS:alembic在使用以下调用运行autogenerate时不会产生任何警告:
alembic-c alembic.ini revision--autogenerate-m“content models”
autogen只是一个方便的功能。它需要生成所有内容,PG的函数索引是目前不支持的,因为SQLAlchemy首先没有反映这些内容。当您运行autogenerate时,每个索引的警告都应该很明显。是否无法让alembic为索引使用文本文字?我不理解这个问题。对不起,让我重新措辞。大概SQLAlchemy需要将索引对象转换为SQL字符串表示来生成索引。由于我使用文本(例如:
text(meta->>'styles')
)定义索引,alembic是否可以在不支持生成PG函数索引的SQL的情况下使用文本?PS:alembic在使用以下调用运行autogenerate时不会产生任何警告:
alembic-c alembic.ini revision--autogenerate-m“content models”