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

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

Closed
noahnu opened this issue Aug 21, 2021 · 3 comments
Closed

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

noahnu opened this issue Aug 21, 2021 · 3 comments
Labels
bug Something isn't working

Comments

@noahnu
Copy link

noahnu commented Aug 21, 2021

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
Copy link
Member

long2ice commented Aug 23, 2021

That's right

@long2ice long2ice added the bug Something isn't working label Aug 23, 2021
long2ice added a commit that referenced this issue Aug 31, 2021
@long2ice
Copy link
Member

Fixed

@noahnu
Copy link
Author

noahnu commented Mar 13, 2022

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"),)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants