Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Repeated Index Migrations with Unchanged Models #363

Open
urszkam opened this issue Nov 13, 2024 · 0 comments
Open

Repeated Index Migrations with Unchanged Models #363

urszkam opened this issue Nov 13, 2024 · 0 comments

Comments

@urszkam
Copy link

urszkam commented Nov 13, 2024

I'm encountering an issue with Aerich where a new migration is generated every time I run docker-compose, even though there have been no changes to my models or indexes. The migrations only contain DROP INDEX and CREATE INDEX statements related to the indexes defined in my model's Meta class.

Environment:

    Aerich version: 0.7.1 (also tested with the latest version)
    Tortoise ORM version: 0.19.3 (also tested with the latest version)
    Python version: 3.11.10
    Database: PostgreSQL 15
    Docker: Using Docker Compose

Model Definition:

class Event(models.Model):
    id = fields.CharField(max_length=50, pk=True)
    field1 = TSVectorField(null=True)
    field2 = fields.DatetimeField(null=True)

    class Meta:
        indexes = [
            GinIndex(fields=("field1",)),
            BrinIndex(fields=("field2",))
        ]

What I've Tried:

  • Assigning specific names to the indexes using the name parameter:
    class Meta:
        indexes = [
            GinIndex(fields=("field1",), name="gin_index_field1"),
            BrinIndex(fields=("field2",), name="brin_index_field2")
        ]

However, this did not resolve the issue.

  • Checked the aerich table in the database:
    The values representing both indexes do not change between migrations.
    It is unclear why Aerich detects changes in the model when there are none.

Generated Migration File (repeatedly created):

async def upgrade(db: BaseDBAsyncClient) -> str:
    return """
        DROP INDEX "brin_index_field2";
        DROP INDEX "gin_index_field1";
        CREATE INDEX "gin_index_field1" ON "event" USING GIN ("field1");
        CREATE INDEX "brin_index_field2" ON "event" USING BRIN ("field2");
    """

async def downgrade(db: BaseDBAsyncClient) -> str:
    return """
        DROP INDEX "brin_index_field2";
        DROP INDEX "gin_index_field1";
        CREATE INDEX "gin_index_field1" ON "event" USING GIN ("field1");
        CREATE INDEX "brin_index_field2" ON "event" USING BRIN ("field2");
    """

Problem Details:
Every time I run docker-compose up, Aerich generates a new migration that includes dropping and recreating the indexes on field1 and field2.
This occurs even when there have been no changes to the model definitions or the database schema.
The issue persists after upgrading to the latest versions of Aerich and Tortoise ORM.
The problem seems related to the use of GinIndex and BrinIndex in the model's Meta class(issue does not exist when I'm creating the indexes via executing a query instead of Meta).

Expected Behavior:
No new migrations should be generated if there are no changes to the models or the indexes.
The indexes should not be dropped and recreated unnecessarily.

Additional Information:
It seems that Aerich is detecting changes in the index definitions each time, possibly due to non-deterministic generation of index representations or internal comparison issues.
The issue might be related to how GinIndex and BrinIndex are compared during the migration detection process.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant