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 bug in schema_generator When modify a unique field #1769

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Fixed
- improve jsonfield type hint (#1700)
- Fix bug in tortoise.models.Model When a QuerySet uses the only function and then uses the print function to print the returned result, an AttributeError is generated (#1724)
- Update the pylint plugin to latest astroid version (#1708)

- Fix bug in schema_generator When modify a unique field (#1768)
Added
^^^^^
- Add POSIX Regex support for PostgreSQL and MySQL (#1714)
Expand Down
2 changes: 1 addition & 1 deletion tortoise/backends/base/schema_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def _get_index_sql(self, model: "Type[Model]", field_names: List[str], safe: boo
)

def _get_unique_index_sql(self, exists: str, table_name: str, field_names: List[str]) -> str:
index_name = self._generate_index_name("uidx", table_name, field_names)
index_name = self._generate_index_name("uid", table_name, field_names)
return self.UNIQUE_INDEX_CREATE_TEMPLATE.format(
exists=exists,
index_name=index_name,
Expand Down
17 changes: 14 additions & 3 deletions tortoise/backends/mysql/schema_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ class MySQLSchemaGenerator(BaseSchemaGenerator):
DIALECT = "mysql"
TABLE_CREATE_TEMPLATE = "CREATE TABLE {exists}`{table_name}` ({fields}){extra}{comment};"
INDEX_CREATE_TEMPLATE = "KEY `{index_name}` ({fields})"
UNIQUE_CONSTRAINT_CREATE_TEMPLATE = "UNIQUE KEY `{index_name}` ({fields})"
UNIQUE_INDEX_CREATE_TEMPLATE = UNIQUE_CONSTRAINT_CREATE_TEMPLATE
FIELD_TEMPLATE = "`{name}` {type} {nullable} {unique}{primary}{comment}{default}"
UNIQUE_INDEX_CREATE_TEMPLATE = 'CREATE UNIQUE INDEX {exists}`{index_name}` ON `{table_name}` ({fields});'
UNIQUE_CONSTRAINT_CREATE_TEMPLATE = UNIQUE_INDEX_CREATE_TEMPLATE
FIELD_TEMPLATE = "`{name}` {type} {nullable} {primary}{comment}{default}"
GENERATED_PK_TEMPLATE = "`{field_name}` {generated_sql}{comment}"
FK_TEMPLATE = (
"{constraint}FOREIGN KEY (`{db_column}`)"
Expand Down Expand Up @@ -101,3 +101,14 @@ def _get_inner_statements(self) -> List[str]:
self._field_indexes.clear()
self._foreign_keys.clear()
return extra

def _get_table_sql(self, model: "Type[Model]", safe: bool = True) -> dict:
sql_info = super()._get_table_sql(model, safe)
table_creation_string = sql_info.get("table_creation_string", "")
for field_name, field in model._meta.fields_map.items():
if field.unique and not field.pk:
unique_index_sql = self._get_unique_index_sql(exists="", table_name=model._meta.db_table,
field_names=[field_name])
table_creation_string += ("\n" + unique_index_sql)
sql_info["table_creation_string"] = table_creation_string
return sql_info
Loading