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

Conversation

gck123
Copy link
Contributor

@gck123 gck123 commented Nov 14, 2024

Here's a detailed description and reproduction of the problem #1768

Description

file: tortoise.backends.base.schema_generator
reason: Make it consistent with the prefixes in add_index and drop_index in aerich.ddl.init
old:

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)
        return self.UNIQUE_INDEX_CREATE_TEMPLATE.format(
            exists=exists,
            index_name=index_name,
            table_name=table_name,
            fields=", ".join([self.quote(f) for f in field_names]),
        )

new:

def _get_unique_index_sql(self, exists: str, table_name: str, field_names: List[str]) -> str:
        index_name = self._generate_index_name("uid", table_name, field_names)
        return self.UNIQUE_INDEX_CREATE_TEMPLATE.format(
            exists=exists,
            index_name=index_name,
            table_name=table_name,
            fields=", ".join([self.quote(f) for f in field_names]),
        )

file: tortoise.backends.mysql.schema_generator
reason:

  1. Delete the unique in FIELD_TEMPLATE because if you don't delete it, you will create an additional unique index but the name will be name, not uid as defined in the code, which will result in two indexes associated with the name field.
  2. Add the _get_table_sql function in order to add the sql for creating the unique index to the create table sql.
  3. Modify UNIQUE_INDEX_CREATE_TEMPLATE because if you use KEY xxx statement to create must be within the create table sql, but in its internal will appear to create the index name does not contain the name of the table, when deleted and will be with the name of the aerich in the name of the inconsistency, it will be an exception!

old:

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}"

new:

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}"

Motivation and Context

If you want to validate my code please make sure that the commit (tortoise/aerich@e971653) mentioned in the re-issue is already in your code.

How Has This Been Tested?

make ci

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.

@gck123 gck123 closed this Nov 14, 2024
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.

1 participant