tortoise / aerich

A database migrations tool for TortoiseORM, ready to production.
https://github.com/tortoise/aerich
Apache License 2.0
820 stars 94 forks source link

No changes detected when adding a composite index to PostgreSQL #193

Closed noahnu closed 2 years ago

noahnu commented 3 years ago

I'm using a postgreSQL database with tortoise orm.

The model:

Before:

class MyModel(Model):
    id = fields.UUIDField(pk=True)
    created_at = fields.DatetimeField(auto_now_add=True)
    soft_deleted = fields.BooleanField(default=False)

    class Meta:
        ordering = ["created_at"]

After:

class MyModel(Model):
    id = fields.UUIDField(pk=True)
    created_at = fields.DatetimeField(auto_now_add=True)
    soft_deleted = fields.BooleanField(default=False)

    class Meta:
        indexes = (("soft_deleted", "created_at"),)
        ordering = ["created_at"]

I then ran aerich migrate --name default and I'm getting a message "No changes detected". I've tried adding indexes to some other tables and it doesn't work. Constraints (i.e. unique_together) seems to work fine.

long2ice commented 3 years ago

That's right

long2ice commented 2 years ago

Fixed

noahnu commented 2 years ago

I tried adding a unique_together composite index, and it did not detect the creation of the index, but it did detect the removal of said index.

I swapped the upgrade and downgrades lines in the generated migration file as a temporary workaround.

Before

class MyModel(Model):
    id = fields.UUIDField(pk=True)
    name = fields.CharField(max_length=255)
    thing = fields.ForeignKeyField(
        "models.SomeForeignTable", related_name="things", on_delete=fields.CASCADE, null=True
    )
    created_at = fields.DatetimeField(auto_now_add=True)
    modified_at = fields.DatetimeField(auto_now=True)

After

class MyModel(Model):
    id = fields.UUIDField(pk=True)
    name = fields.CharField(max_length=255)
    thing = fields.ForeignKeyField(
        "models.SomeForeignTable", related_name="things", on_delete=fields.CASCADE, null=True
    )
    created_at = fields.DatetimeField(auto_now_add=True)
    modified_at = fields.DatetimeField(auto_now=True)

    class Meta:
        indexes = (("thing_id", "name"),)
        unique_together = (("thing_id", "name"),)