fix: init-db checks aerich table instead of migrations folder (#267)#526
Conversation
Why this fix matters — Aerich migration workflowThe intended workflow (now works correctly)Aerich is designed to work like Django migrations or Go ORM (GORM AutoMigrate + goose): Local development# 1. Start local database docker compose up -d # 2. First time only — initialise aerich and create the first migration aerich init -t your_app.config.TORTOISE_ORM aerich init-db # creates migrations/models/0_..._init.py AND applies it locally # 3. After changing your models aerich migrate --name add_user_email # generates migrations/models/1_..._add_user_email.py aerich upgrade # apply locally to verify it works # 4. Commit the generated migration files git add migrations/ git commit -m "add user email field" git pushThe Another developer onboards (the bug this PR fixes)Before this fix, a new developer who cloned the repo would hit a confusing error: This happened because aerich was checking whether the folder existed, not whether the After this fix: # Developer 2 clones the repo — migrations/ is already there from git git clone https://github.com/your-org/your-app.git docker compose up -d aerich init-db # Applied existing migrations to database for app "models" # Success writing schemas to database "db.sqlite3" # From this point, normal workflow applies: aerich upgrade # picks up any migrations added since the cloneServer / CI / GitOps# First deploy to a fresh environment aerich init-db # applies all migrations in order, marks them as applied # Every subsequent deploy (GitOps / CD pipeline) aerich upgrade # only runs migrations not yet recorded in the aerich table
Comparison with other frameworks
All of them share the same principle: migration files are the source of truth, committed to |
| in order to workaround the bug |
Description
Fixes #267
What changed
aerich init-dbpreviously checked whether the migrations folder existed to decide if the app was already initialised. This caused a false positive when migration files were committed to a repo: new developers (or CI servers) runningaerich init-dbagainst a fresh database would get:The fix replaces the folder check with a database-side check:
aerich init-dbnow queries theaerichtracking table to determine prior initialisation.New behaviour
aerichtable has records)Usage