Skip to content

[Question] How to add a unique index to definition for PGMaterializedView #94

@MeganBeckett

Description

@MeganBeckett

Hi there

I have a materialized view defined using PGMaterializedView in a views.py file. The views had already been created in the database.

I want to now add a unique index to my view - can I do this in the definition of a PGMaterializedView and manage the update with alembic?

As a minimal example:

my_view = PGMaterializedView (
    schema="public",
    signature="vw_period_totals",
    definition="""
        SELECT 
            id, 
            name,
            period,
            sum(count) AS total
        FROM table
        GROUP BY id, name, period
    """,
    with_data=True
)

I now want to add a unique index to the view and have this managed by alembic, namely:

CREATE unique index vw_period_totals_index ON vw_period_totals (name, period);

I can't add this to the definition argument. So, is there some way to manage this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions