Skip to content

Fix AlterField ignoring max_length changes in migrations#2128

Merged
abondar merged 1 commit intotortoise:developfrom
Br1an67:fix/alter-field-max-length
Mar 3, 2026
Merged

Fix AlterField ignoring max_length changes in migrations#2128
abondar merged 1 commit intotortoise:developfrom
Br1an67:fix/alter-field-max-length

Conversation

@Br1an67
Copy link
Contributor

@Br1an67 Br1an67 commented Mar 1, 2026

Description

The _alter_field method 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 changing max_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 ALTER statements when the column type changes.

Motivation and Context

Fixes #2120

When a user changes max_length on a CharField and generates a migration, Tortoise correctly creates an AlterField operation. 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 — verifies ALTER TABLE ... ALTER COLUMN ... TYPE VARCHAR(64) is generated
  • test_mysql_alter_field_max_length — verifies ALTER TABLE ... MODIFY COLUMN ... VARCHAR(64) NULL is generated
  • test_mssql_alter_field_max_length — verifies ALTER TABLE ... ALTER COLUMN ... VARCHAR(64) NULL is generated

All existing schema editor tests continue to pass (69 total).

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.

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
@codspeed-hq
Copy link

codspeed-hq bot commented Mar 3, 2026

Merging this PR will not alter performance

✅ 24 untouched benchmarks


Comparing Br1an67:fix/alter-field-max-length (282fa95) with develop (314273c)

Open in CodSpeed

@abondar abondar merged commit 5d08bc2 into tortoise:develop Mar 3, 2026
25 checks passed
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.

AlterField does not update CharField.max_length (PostgreSQL backend → (no-op) generated)

2 participants