Skip to content

Conversation

@chuwei-dev
Copy link
Contributor

Description

This PR updates migration file writing to explicitly use UTF-8 encoding instead of ASCII.

Previously, migration files were written with:

path.write_text(self.as_string(), encoding="ascii")

This prevented writing non-ASCII characters and could raise:

UnicodeEncodeError: 'ascii' codec can't encode characters

The implementation now uses:

path.write_text(self.as_string(), encoding="utf-8")

Motivation and Context

Fixes #2096.

Migration files may contain non-ASCII characters (e.g. verbose names, comments, or field descriptions).
Using a hard-coded ASCII encoding makes such content impossible to write.

UTF-8 is the standard encoding for Python source files and ensures:

  • Cross-platform consistency
  • Deterministic migration output
  • No reliance on system locale defaults

This change removes the unnecessary ASCII restriction and improves robustness.


How Has This Been Tested?

Tested locally with:

  • Python 3.12
  • Windows 10 environment

Steps performed:

  1. Created a model containing non-ASCII characters (e.g. Chinese verbose_name).
  2. Ran tortoise makemigrations.
  3. Confirmed that migration files are generated successfully.
  4. Verified that no encoding errors occur.
  5. Ensured existing migrations continue to work correctly.

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.

@abondar
Copy link
Member

abondar commented Feb 11, 2026

Please fix linter

@codspeed-hq
Copy link

codspeed-hq bot commented Feb 11, 2026

Merging this PR will not alter performance

✅ 24 untouched benchmarks


Comparing chuwei-dev:fix/migrations-loader-default-encoding (c279796) with develop (fe73270)

Open in CodSpeed

@coveralls
Copy link

coveralls commented Feb 11, 2026

Pull Request Test Coverage Report for Build 21899166276

Details

  • 1 of 1 (100.0%) changed or added relevant line in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 82.478%

Totals Coverage Status
Change from base Build 21856593682: 0.0%
Covered Lines: 10420
Relevant Lines: 12167

💛 - Coveralls

@chuwei-dev
Copy link
Contributor Author

Thanks! Formatting issues have been fixed.

@abondar abondar merged commit 48151d6 into tortoise:develop Feb 11, 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.

UnicodeEncodeError when running tortoise makemigrations due to ASCII encoding in migration writer

3 participants