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

fix index name #1617

Merged
merged 1 commit into from
May 18, 2024
Merged

fix index name #1617

merged 1 commit into from
May 18, 2024

Conversation

Abdeldjalil-H
Copy link
Contributor

Use index_name from class Index to get index name instead of BaseSchemaGenerator._generate_index_name.

Motivation and Context

This allows you to create your custom index class that inherits from Index and override index_name method to set index name.

How Has This Been Tested?

On existing tests.

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added the changelog accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@coveralls
Copy link

Pull Request Test Coverage Report for Build 9135160308

Details

  • 1 of 3 (33.33%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.01%) to 87.945%

Changes Missing Coverage Covered Lines Changed/Added Lines %
tortoise/indexes.py 1 3 33.33%
Totals Coverage Status
Change from base Build 9080530396: 0.01%
Covered Lines: 5786
Relevant Lines: 6483

💛 - Coveralls

@abondar
Copy link
Member

abondar commented May 18, 2024

Is there reason why defining name for Index class doesn't work for you?

return self.INDEX_CREATE_TEMPLATE.format(
exists="IF NOT EXISTS " if safe else "",
index_name=self.index_name(schema_generator, model),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here the method index_name is called, but for the first case the code of index_name is used instead of calling the method.

@Abdeldjalil-H
Copy link
Contributor Author

In my case I am using a custom index class:

from tortoise.indexes import Index

class MyCustomIndex(Index):
    PREFIX = "idx"
    def index_name(self, schema_generator: BaseSchemaGenerator, model: models.Model):
        if self.name:
            return self.name

        table_name = model._meta.db_table

        return f"{self.PREFIX}_{table_name}_{'_'.join(self.fields)}".lower()[:64]

Then I wrap all my indexes with this custom class so the name is generated automatically. But when I run the migrations, I still get the old name.

By the way, this is not the case if you pass expressions instead of fields as you can see in the previous code.

@abondar abondar merged commit 0d5b950 into tortoise:develop May 18, 2024
7 checks passed
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

Successfully merging this pull request may close these issues.

None yet

3 participants