{"record":{"id":"17b89bfa8e642096","repo":"pocketbase/pocketbase","slug":"failed-to-apply-migration-s-w","errorCode":null,"errorMessage":"failed to apply migration %s: %w","messagePattern":"failed to apply migration (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"core/migrations_runner.go","lineNumber":154,"sourceCode":"\t\t\t\t\t}\n\n\t\t\t\t\tshouldReapply, err := m.ReapplyCondition(txApp, r, m.File)\n\t\t\t\t\tif err != nil {\n\t\t\t\t\t\treturn err\n\t\t\t\t\t}\n\t\t\t\t\tif !shouldReapply {\n\t\t\t\t\t\tcontinue\n\t\t\t\t\t}\n\n\t\t\t\t\t// clear previous history stored entry\n\t\t\t\t\t// (it will be recreated after successful execution)\n\t\t\t\t\tr.saveRevertedMigration(txApp, m.File)\n\t\t\t\t}\n\n\t\t\t\t// ignore empty Up action\n\t\t\t\tif m.Up != nil {\n\t\t\t\t\tif err := m.Up(txApp); err != nil {\n\t\t\t\t\t\treturn fmt.Errorf(\"failed to apply migration %s: %w\", m.File, err)\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tif err := r.saveAppliedMigration(txApp, m.File); err != nil {\n\t\t\t\t\treturn fmt.Errorf(\"failed to save applied migration info for %s: %w\", m.File, err)\n\t\t\t\t}\n\n\t\t\t\tapplied = append(applied, m.File)\n\t\t\t}\n\n\t\t\treturn nil\n\t\t})\n\t})\n\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\treturn applied, nil","sourceCodeStart":136,"sourceCodeEnd":172,"githubUrl":"https://github.com/pocketbase/pocketbase/blob/5d217ddb50cb144d80a5d0b0bdf11b52b2c3e457/core/migrations_runner.go#L136-L172","documentation":"Thrown when a migration's Up function returns an error while the runner applies it inside a transaction. The migration file name is included. Because it happens inside RunInTransaction, the whole batch's changes for that transaction roll back; the migration is not recorded as applied.","triggerScenarios":"Running migrate up where a migration's Go code fails: invalid SQL inside m.Up(txApp), unique constraint violation, table already exists (migration not idempotent when re-applied), or external state the migration depends on being absent.","commonSituations":"Deploying a new migration to a DB where the schema was already changed by hand; migrations written against an older schema version; data-backfill migrations hitting duplicate keys; SQLite locking during long migrations.","solutions":["Read the wrapped error — it is whatever the migration's Up returned, pinpointing the failing statement.","Make migrations idempotent (IF NOT EXISTS, IF EXISTS) so re-runs and drifted schemas don't break them.","Fix the DB drift the migration assumed away (e.g. drop the manually created table or column) and re-run.","Use 'migrate down' for the failing migration only if it has a Down and its changes partially applied outside a transaction context."],"exampleFix":"// before: non-idempotent migration\nm.Up = func(db core.App) error {\n    _, err := db.DB().Exec(\"CREATE TABLE reports (...)\")\n    return err\n}\n\n// after\nm.Up = func(db core.App) error {\n    _, err := db.DB().Exec(\"CREATE TABLE IF NOT EXISTS reports (...)\")\n    return err\n}","handlingStrategy":"try-catch","validationCode":"// pre-flight: verify schema assumptions the migration makes\nvar n int\napp.DB().NewQuery(\"SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='reports'\").Row(&n)\nif n > 0 && migrationCreatesReports {\n    return fmt.Errorf(\"table reports already exists; fix drift before migrating\")\n}","typeGuard":null,"tryCatchPattern":"_, err := runner.Up()\nif err != nil {\n    var me *migrationApplyError // if you wrap it; otherwise match the message prefix\n    if errors.As(err, &me) || strings.Contains(err.Error(), \"failed to apply migration\") {\n        // transaction rolled back; fix the wrapped cause (often constraint/drift),\n        // make the migration idempotent, then re-run migrate up\n    }\n}","preventionTips":["Write idempotent migrations (IF NOT EXISTS / IF EXISTS).","Test migrations on a copy of production pb_data before deploying.","Never let humans hot-fix schemas that migrations will later assume control of."],"tags":["migrations","database","deployment"],"backgroundTag":null,"analyzedSha":"5d217ddb50cb144d80a5d0b0bdf11b52b2c3e457","analyzedAt":"2026-08-15T10:06:33.165Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}