Skip to content

fix: aerich migrate crashes without init-db & aerich init-db should create folder after validating app #443

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

Merged
merged 4 commits into from
May 8, 2025

Conversation

hail-pas
Copy link
Contributor

📌 Issue: Two Problems with aerich migrate and init-db Behavior

💥 Problem 1: aerich migrate crashes if init-db was not run

Steps to Reproduce:

  1. Run aerich init
  2. Do not run aerich init-db
  3. Run aerich migrate

Observed Error:

File "/path/to/aerich/migrate.py", line 176, in migrate
    cls.diff_models(last_version, new_version_content)
File "/path/to/aerich/migrate.py", line 390, in diff_models
    old_models.pop(_aerich, None)
AttributeError: 'NoneType' object has no attribute 'pop'

💥 Problem 2: aerich init-db should create folder before validating app

Steps to Reproduce:

  1. Add an invalid or non-existent app to the Aerich config
  2. Run aerich --app non_existed init-db

Observed Error:

Error: Can't get app named 'non_existed'  # but the migration folder created

@waketzheng
Copy link
Contributor

waketzheng commented Apr 20, 2025

Hi, @hail-pas
How to reproduce problem 1?
I got: Error: You need to run aerich init-db first to initialize the database. with the following steps:

echo 'from __future__ import annotations

from tortoise import Model, fields

TORTOISE_ORM = {
    "connections": {"default": "sqlite://db.sqlite3"},
    "apps": {"models": {"models": ["models", "aerich.models"]}},
}


class Offer(Model):
    name = fields.CharField(max_length=20)
' > models.py
aerich init -t models.TORTOISE_ORM
aerich migrate

And for problem2, I got:

(venv) mac10.12 in ~/trying/something λ aerich --app not_exists init-db
/Users/mac10.12/trying/something/venv/lib/python3.11/site-packages/tortoise/__init__.py:514: RuntimeWarning: Module "models" has no models
  cls._init_apps(apps_config)
Success creating app migration folder migrations/not_exists
Success generating initial migration file for app "not_exists"
(venv) mac10.12 in ~/trying/something λ ls migrations
not_exists

settings:

TORTOISE_ORM = {
    "connections": {"default": "sqlite://db.sqlite3"},
    "apps": {
        "models": {"models": ["models", "aerich.models"]},
        "not_exists": {"models": ["models"]},
    },
}

@waketzheng waketzheng merged commit e7d8ab5 into tortoise:dev May 8, 2025
15 checks passed
@waketzheng waketzheng mentioned this pull request May 12, 2025
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.

2 participants