Skip to content

Comparing computed fields throws warn #1151

@hevalhazalkurt

Description

@hevalhazalkurt

I'm not sure if this is a bug or a mistake caused by me, but I wanted to share it here because I couldn't find a proper solution.

I have a computed tsvector field in my database as you can see in the section of the migration file and model I shared below.

def upgrade():
    op.create_table(
        "a_table",
        sa.Column("Column1", sa.String(), nullable=False),
        sa.Column("Column2", sa.ARRAY(sa.String(), dimensions=1), nullable=True),
        sa.Column("Column3", sa.ARRAY(sa.String(), dimensions=1), nullable=True),
        sa.Column("Column4", sa.ARRAY(sa.String(), dimensions=1), nullable=True),
        sa.Column("Column5", sa.ARRAY(sa.String(), dimensions=1), nullable=True),
        sa.Column("Column6", sa.ARRAY(sa.String(), dimensions=1), nullable=True),
    )
    op.execute(
        """
        CREATE OR REPLACE FUNCTION immutable_array_to_string(text[], text)
        RETURNS text as $$ SELECT array_to_string($1, $2); $$
        LANGUAGE sql IMMUTABLE"""
    )
    op.execute(
        """
        ALTER TABLE elementsearchindex
        ADD COLUMN new_column tsvector GENERATED ALWAYS
        AS (to_tsvector('english', Column1
                        || ' ' || immutable_array_to_string(coalesce(Column2, '{}'), ' ')
                        || ' ' || immutable_array_to_string(coalesce(Column3, '{}'), ' ')
                        || ' ' || immutable_array_to_string(coalesce(Column4, '{}'), ' ')
                        || ' ' || immutable_array_to_string(coalesce(Column5, '{}'), ' ')
                        || ' ' || immutable_array_to_string(coalesce(Column6, '{}'), ' ')
                        )
                    ) STORED
        """
    )
class TSVector(TypeDecorator):
    impl = TSVECTOR

class ATable(Base):
    Column1 = Column(String, nullable=False, index=True)
    Column2 = Column(ARRAY(String, dimensions=1), nullable=True, index=True)
    Column3 = Column(ARRAY(String, dimensions=1), nullable=True, index=True)
    Column4 = Column(ARRAY(String, dimensions=1), nullable=True, index=True)
    Column5 = Column(ARRAY(String, dimensions=1), nullable=True, index=True)
    Column6 = Column(ARRAY(String, dimensions=1), nullable=True, index=True)

    new_column = Column(
        TSVector(),
        Computed(
            """to_tsvector('english', Column1
                        || ' ' || immutable_array_to_string(coalesce(Column2, '{}'), ' ')
                        || ' ' || immutable_array_to_string(coalesce(Column3, '{}'), ' ')
                        || ' ' || immutable_array_to_string(coalesce(Column4, '{}'), ' ')
                        || ' ' || immutable_array_to_string(coalesce(Column5, '{}'), ' ')
                        || ' ' || immutable_array_to_string(coalesce(Column6, '{}'), ' ')
                        )""",
            persisted=True,
        ),
        nullable=True,
        index=True,
    )

I do not have any problems during the upgrade or downgrade. Likewise, the system is working properly. But whenever I want to do any database update I see the following warning.

alembic/autogenerate/compare.py:1090: UserWarning: Computed default on a_table.new_field cannot be modified

I can see the need of throwing message when there is a change but no idea why comparison think there are some changes.

Versions.

  • OS: Monterey 12.1
  • Python: 3.10.6
  • Alembic: 1.8.1
  • SQLAlchemy: 1.4.35
  • Database: Postgres 14

Thank you!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions