|
4 | 4 | from hashlib import sha256
|
5 | 5 | from typing import TYPE_CHECKING, Any, Type, cast
|
6 | 6 |
|
7 |
| -from pypika_tortoise.context import DEFAULT_SQL_CONTEXT |
8 |
| - |
9 | 7 | from tortoise.exceptions import ConfigurationError
|
10 | 8 | from tortoise.fields import JSONField, TextField, UUIDField
|
11 | 9 | from tortoise.fields.relational import OneToOneFieldInstance
|
@@ -348,31 +346,17 @@ def _get_table_sql(self, model: "Type[Model]", safe: bool = True) -> dict:
|
348 | 346 |
|
349 | 347 | if model._meta.indexes:
|
350 | 348 | for index in model._meta.indexes:
|
351 |
| - if not isinstance(index, Index): |
| 349 | + if isinstance(index, Index): |
| 350 | + idx_sql = index.get_sql(self, model, safe) |
| 351 | + else: |
352 | 352 | fields = []
|
353 | 353 | for field in index:
|
354 | 354 | field_object = model._meta.fields_map[field]
|
355 | 355 | fields.append(field_object.source_field or field)
|
| 356 | + idx_sql = self._get_index_sql(model, fields, safe=safe) |
356 | 357 |
|
357 |
| - _indexes.append(self._get_index_sql(model, fields, safe=safe)) |
358 |
| - else: |
359 |
| - if index.fields: |
360 |
| - fields = [f for f in index.fields] |
361 |
| - elif index.expressions: |
362 |
| - fields = [ |
363 |
| - f"({expression.get_sql(DEFAULT_SQL_CONTEXT)})" |
364 |
| - for expression in index.expressions |
365 |
| - ] |
366 |
| - else: |
367 |
| - raise ConfigurationError( |
368 |
| - "At least one field or expression is required to define an index." |
369 |
| - ) |
370 |
| - |
371 |
| - _indexes.append( |
372 |
| - self._get_index_sql( |
373 |
| - model, fields, safe=safe, index_type=index.INDEX_TYPE, extra=index.extra |
374 |
| - ) |
375 |
| - ) |
| 358 | + if idx_sql: |
| 359 | + _indexes.append(idx_sql) |
376 | 360 |
|
377 | 361 | field_indexes_sqls = [val for val in list(dict.fromkeys(_indexes)) if val]
|
378 | 362 |
|
|
0 commit comments