{"record":{"id":"97b6303bd93f0689","repo":"hasura/graphql-engine","slug":"v-not-applied-on-database","errorCode":null,"errorMessage":"%v not applied on database","messagePattern":"(.+?) not applied on database","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"cli/migrate/migrate.go","lineNumber":2199,"sourceCode":"\n\t\t\t\tret <- migr\n\t\t\t\tgo func(migr *Migration, m *Migrate) {\n\t\t\t\t\terr := migr.Buffer()\n\t\t\t\t\tif err != nil {\n\t\t\t\t\t\tm.Logger.Error(err)\n\t\t\t\t\t}\n\t\t\t\t}(migr, m)\n\n\t\t\t\tfrom = database.NilVersion\n\n\t\t\t\tcontinue\n\t\t\t} else if err != nil {\n\t\t\t\tret <- err\n\n\t\t\t\treturn\n\t\t\t}\n\n\t\t\tret <- fmt.Errorf(\"%v not applied on database\", prev)\n\n\t\t\treturn\n\t\t}\n\n\t\tmigr, err := m.newMigration(suint64(from), int64(prev.Version))\n\t\tif err != nil {\n\t\t\tret <- err\n\n\t\t\treturn\n\t\t}\n\n\t\tret <- migr\n\t\tgo func(migr *Migration, m *Migrate) {\n\t\t\terr := migr.Buffer()\n\t\t\tif err != nil {\n\t\t\t\tm.Logger.Error(err)\n\t\t\t}\n\t\t}(migr, m)","sourceCodeStart":2181,"sourceCodeEnd":2217,"githubUrl":"https://github.com/hasura/graphql-engine/blob/724551b9ae87845594ef0408cff0e50eb6c90dc5/cli/migrate/migrate.go#L2181-L2217","documentation":"Thrown during a down-migration when the CLI rewinds migrations: after applying version `from`, it asks the database driver for the previous applied version (Prev). If the database has no previous version but the migration source does report one (and the target isn't -1), the database and filesystem migration histories have diverged and the migration is aborted with '<version> not applied on database'. It protects against rolling back migrations the database doesn't know about.","triggerScenarios":"Running `hasura migrate downgrade`/`rollback` (Migrate.Down path in cli/migrate/migrate.go) when the versions recorded in hdb_catalog.schema_migrations / catalog state don't match the migrations present in the migrations/ directory — e.g. someone deleted rows from the migrations table or copied migrations files from another project.","commonSituations":"Manually editing or truncating the schema_migrations table; restoring a database dump without the migrations metadata; copying the migrations folder between environments; running downgrade after a partial/failed migration run.","solutions":["Compare `hasura migration status` output with files in migrations/ and fix the divergence (delete stray files or re-insert missing version rows)","If the database is truly behind, run `hasura migrate apply` to bring it up to date before downgrading","As a last resort, delete the migrations directory's extra versions or squash migrations and re-baseline the database (drop/recreate schema_migrations entries)","Avoid editing hdb_catalog.schema_migrations by hand; use CLI commands only"],"exampleFix":"# before\nhasura migrate downgrade --to 12  # database history diverged from migrations/\n\n# after\nhasura migration status          # inspect divergence first\nhasura migrate apply              # sync database, then downgrade","handlingStrategy":"validation","validationCode":"// Before migrating, compare applied versions with source versions\nstatus, err := m.GetStatus() // or `hasura migration status`\n// ensure every version <= target exists in both source and database","typeGuard":null,"tryCatchPattern":"if err := migrateInstance.Down(ctx, to); err != nil {\n    if strings.Contains(err.Error(), \"not applied on database\") {\n        // database/source histories diverged: run migration status and reconcile\n    }\n}","preventionTips":["Never edit hdb_catalog.schema_migrations manually","Use only CLI commands to create/apply/rollback migrations","Run `hasura migration status` in CI before apply/downgrade","Keep the migrations directory in version control and identical across environments"],"tags":["migration","database-state","hasura","divergence"],"backgroundTag":"migration-state-mismatch","analyzedSha":"724551b9ae87845594ef0408cff0e50eb6c90dc5","analyzedAt":"2026-08-28T07:32:55.105Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}