Skip to content

Backend specific migration fixes#2109

Merged
abondar merged 1 commit intodevelopfrom
fix/migration-fixes
Feb 15, 2026
Merged

Backend specific migration fixes#2109
abondar merged 1 commit intodevelopfrom
fix/migration-fixes

Conversation

@abondar
Copy link
Member

@abondar abondar commented Feb 15, 2026

Resolved #2108 and some other backend specific issues

AI summary:
This pull request introduces several improvements and new features to the migrations and schema editing system, with a focus on enhanced cross-database compatibility, new constraint management, and improved test coverage. The most significant changes include the addition of a dialect-aware random hex default generator, support for named unique constraints in migrations, enhancements to schema editors for MySQL and MSSQL, and comprehensive tests for unique constraint handling.

Major Features and Enhancements

  • Added a new RandomHex dialect-aware SqlDefault subclass to generate random hex strings as default values across all supported backends, replacing backend-specific SQL expressions. (RandomHex) is now used in both models and migrations for fields like tracking_id. [1] [2] [3] [4] [5]
  • Introduced support for Meta.constraints on models, enabling named UniqueConstraint objects to be captured by the migration autodetector. This allows for automatic generation of AddConstraint and RemoveConstraint operations in migrations. [1] [2] [3]

Schema Editor and Backend Improvements

  • Enhanced MySQL and MSSQL schema editors:
    • MySQL: _alter_field now uses MODIFY COLUMN for NULL/NOT NULL changes and backtick-quoted templates.
    • MSSQL: _alter_field uses ALTER COLUMN for nullability, manages named default constraints, uses bracket-quoted templates, and downgrades self-referencing FK CASCADE to NO ACTION.

Migration and Model Changes

  • Updated migrations to use dialect-specific SQL for computed fields and switched from RunSQL to RunPython for better backend compatibility. [1] [2] [3]
  • Adjusted the order of model creation in migrations for consistency and correctness. [1] [2]

Testing Improvements

  • Added comprehensive tests for unique constraint handling, including:
    • Inline unique constraints in CREATE TABLE statements.
    • Proper dropping of unique constraints when altering fields from unique to non-unique.
    • Backend-specific introspection and fallback behavior for constraint names in PostgreSQL and MySQL. [1] [2] [3]

Documentation

  • Updated the CHANGELOG.rst to document all new features, backend improvements, and bug fixes included in this release.

@codspeed-hq
Copy link

codspeed-hq bot commented Feb 15, 2026

Merging this PR will not alter performance

✅ 24 untouched benchmarks


Comparing fix/migration-fixes (8edfc14) with develop (3495562)

Open in CodSpeed

@coveralls
Copy link

Pull Request Test Coverage Report for Build 22035846783

Details

  • 128 of 140 (91.43%) changed or added relevant lines in 12 files are covered.
  • 3 unchanged lines in 1 file lost coverage.
  • Overall coverage increased (+0.1%) to 82.661%

Changes Missing Coverage Covered Lines Changed/Added Lines %
tortoise/migrations/schema_editor/mssql.py 50 51 98.04%
tortoise/migrations/schema_editor/oracle.py 7 8 87.5%
tortoise/migrations/schema_editor/sqlite.py 12 13 92.31%
tortoise/migrations/schema_generator/state.py 1 2 50.0%
tortoise/fields/db_defaults.py 10 14 71.43%
tortoise/migrations/schema_editor/mysql.py 21 25 84.0%
Files with Coverage Reduction New Missed Lines %
tortoise/migrations/schema_editor/base.py 3 78.85%
Totals Coverage Status
Change from base Build 22013708504: 0.1%
Covered Lines: 10708
Relevant Lines: 12481

💛 - Coveralls

@abondar abondar merged commit 30b55c5 into develop Feb 15, 2026
45 of 48 checks passed
@abondar abondar deleted the fix/migration-fixes branch February 15, 2026 17:00
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.

OperationalError: constraint does not exist when changing unique=True to unique=False in migration

2 participants