Fix AlterField ignoring max_length changes in migrations#2128
Merged
abondar merged 1 commit intotortoise:developfrom Mar 3, 2026
Merged
Fix AlterField ignoring max_length changes in migrations#2128abondar merged 1 commit intotortoise:developfrom
abondar merged 1 commit intotortoise:developfrom
Conversation
The _alter_field method only checked for nullability, index, unique, description, default and rename changes. It did not compare SQL_TYPE, so changing max_length (e.g. VARCHAR(32) to VARCHAR(64)) produced no migration SQL. Add SQL type comparison to the base schema editor and update MySQL, MSSQL, and Oracle overrides to emit the correct ALTER statements when the column type changes. Fixes tortoise#2120
abondar
approved these changes
Mar 3, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
The
_alter_fieldmethod in the migration schema editor only checked for changes to nullability, index, unique, description, default, and rename. It did not compare the SQL column type, so changingmax_length(e.g.VARCHAR(32)→VARCHAR(64)) produced no migration SQL and the database schema was left out of sync.This PR adds SQL type comparison to the base schema editor and updates the MySQL, MSSQL, and Oracle overrides to emit the correct
ALTERstatements when the column type changes.Motivation and Context
Fixes #2120
When a user changes
max_lengthon aCharFieldand generates a migration, Tortoise correctly creates anAlterFieldoperation. However, applying that migration does nothing because_alter_field()sees no difference in the properties it checks and returns early.How Has This Been Tested?
Added three new tests in
tests/migrations/test_schema_editor_backends.py:test_postgres_alter_field_max_length— verifiesALTER TABLE ... ALTER COLUMN ... TYPE VARCHAR(64)is generatedtest_mysql_alter_field_max_length— verifiesALTER TABLE ... MODIFY COLUMN ... VARCHAR(64) NULLis generatedtest_mssql_alter_field_max_length— verifiesALTER TABLE ... ALTER COLUMN ... VARCHAR(64) NULLis generatedAll existing schema editor tests continue to pass (69 total).
Checklist: