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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Fixed
- Fix `optional` parameter in `pydantic_model_creator` does not work for pydantic v2. (#1551)
- Fix `get_annotations` now evaluates annotations in the default scope instead of the app namespace. (#1552)
- Fix `get_or_create` method. (#1404)
- Use `index_name` instead of `BaseSchemaGenerator._generate_index_name` to generate index name.

Changed
^^^^^^^
Expand Down
19 changes: 6 additions & 13 deletions tortoise/indexes.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,17 @@ def __init__(

def get_sql(self, schema_generator: "BaseSchemaGenerator", model: "Type[Model]", safe: bool):
if self.fields:
return self.INDEX_CREATE_TEMPLATE.format(
exists="IF NOT EXISTS " if safe else "",
index_name=schema_generator.quote(
self.name or schema_generator._generate_index_name("idx", model, self.fields)
),
index_type=f" {self.INDEX_TYPE} ",
table_name=schema_generator.quote(model._meta.db_table),
fields=", ".join(schema_generator.quote(f) for f in self.fields),
extra=self.extra,
)
fields = ", ".join(schema_generator.quote(f) for f in self.fields)
else:
expressions = [f"({expression.get_sql()})" for expression in self.expressions]
fields = ", ".join(expressions)

expressions = [f"({expression.get_sql()})" for expression in self.expressions]
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.

index_name=schema_generator.quote(self.index_name(schema_generator, model)),
index_type=f" {self.INDEX_TYPE} ",
table_name=schema_generator.quote(model._meta.db_table),
fields=", ".join(expressions),
fields=fields,
extra=self.extra,
)

Expand Down